You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
1.0 KiB

8 years ago
8 years ago
8 years ago
  1. package core
  2. import (
  3. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  4. "github.com/tendermint/tendermint/rpc/lib/types"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. func Subscribe(wsCtx rpctypes.WSRPCContext, event string) (*ctypes.ResultSubscribe, error) {
  8. logger.Info("Subscribe to event", "remote", wsCtx.GetRemoteAddr(), "event", event)
  9. types.AddListenerForEvent(wsCtx.GetEventSwitch(), wsCtx.GetRemoteAddr(), event, func(msg types.TMEventData) {
  10. // NOTE: EventSwitch callbacks must be nonblocking
  11. // NOTE: RPCResponses of subscribed events have id suffix "#event"
  12. tmResult := &ctypes.ResultEvent{event, msg}
  13. wsCtx.TryWriteRPCResponse(rpctypes.NewRPCResponse(wsCtx.Request.ID+"#event", tmResult, ""))
  14. })
  15. return &ctypes.ResultSubscribe{}, nil
  16. }
  17. func Unsubscribe(wsCtx rpctypes.WSRPCContext, event string) (*ctypes.ResultUnsubscribe, error) {
  18. logger.Info("Unsubscribe to event", "remote", wsCtx.GetRemoteAddr(), "event", event)
  19. wsCtx.GetEventSwitch().RemoveListenerForEvent(event, wsCtx.GetRemoteAddr())
  20. return &ctypes.ResultUnsubscribe{}, nil
  21. }