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.

25 lines
1.1 KiB

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