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.

27 lines
1.3 KiB

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