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.1 KiB

8 years ago
  1. package core
  2. import (
  3. "github.com/tendermint/go-rpc/types"
  4. ctypes "github.com/tendermint/tendermint/rpc/core/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. 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.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. log.Notice("Unsubscribe to event", "remote", wsCtx.GetRemoteAddr(), "event", event)
  19. wsCtx.GetEventSwitch().RemoveListenerForEvent(event, wsCtx.GetRemoteAddr())
  20. return &ctypes.ResultUnsubscribe{}, nil
  21. }