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.

78 lines
2.4 KiB

7 years ago
8 years ago
8 years ago
8 years ago
  1. package core
  2. import (
  3. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  4. rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. // Subscribe for events via WebSocket.
  8. //
  9. // ```go
  10. // import "github.com/tendermint/tendermint/types"
  11. //
  12. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  13. // result, err := client.AddListenerForEvent(types.EventStringNewBlock())
  14. // ```
  15. //
  16. // > The above command returns JSON structured like this:
  17. //
  18. // ```json
  19. // {
  20. // "error": "",
  21. // "result": {},
  22. // "id": "",
  23. // "jsonrpc": "2.0"
  24. // }
  25. // ```
  26. //
  27. // ### Query Parameters
  28. //
  29. // | Parameter | Type | Default | Required | Description |
  30. // |-----------+--------+---------+----------+-------------|
  31. // | event | string | "" | true | Event name |
  32. //
  33. // <aside class="notice">WebSocket only</aside>
  34. func Subscribe(wsCtx rpctypes.WSRPCContext, event string) (*ctypes.ResultSubscribe, error) {
  35. logger.Info("Subscribe to event", "remote", wsCtx.GetRemoteAddr(), "event", event)
  36. types.AddListenerForEvent(wsCtx.GetEventSwitch(), wsCtx.GetRemoteAddr(), event, func(msg types.TMEventData) {
  37. // NOTE: EventSwitch callbacks must be nonblocking
  38. // NOTE: RPCResponses of subscribed events have id suffix "#event"
  39. tmResult := &ctypes.ResultEvent{event, msg}
  40. wsCtx.TryWriteRPCResponse(rpctypes.NewRPCSuccessResponse(wsCtx.Request.ID+"#event", tmResult))
  41. })
  42. return &ctypes.ResultSubscribe{}, nil
  43. }
  44. // Unsubscribe from events via WebSocket.
  45. //
  46. // ```go
  47. // import 'github.com/tendermint/tendermint/types'
  48. //
  49. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  50. // result, err := client.RemoveListenerForEvent(types.EventStringNewBlock())
  51. // ```
  52. //
  53. // > The above command returns JSON structured like this:
  54. //
  55. // ```json
  56. // {
  57. // "error": "",
  58. // "result": {},
  59. // "id": "",
  60. // "jsonrpc": "2.0"
  61. // }
  62. // ```
  63. //
  64. // ### Query Parameters
  65. //
  66. // | Parameter | Type | Default | Required | Description |
  67. // |-----------+--------+---------+----------+-------------|
  68. // | event | string | "" | true | Event name |
  69. //
  70. // <aside class="notice">WebSocket only</aside>
  71. func Unsubscribe(wsCtx rpctypes.WSRPCContext, event string) (*ctypes.ResultUnsubscribe, error) {
  72. logger.Info("Unsubscribe to event", "remote", wsCtx.GetRemoteAddr(), "event", event)
  73. wsCtx.GetEventSwitch().RemoveListenerForEvent(event, wsCtx.GetRemoteAddr())
  74. return &ctypes.ResultUnsubscribe{}, nil
  75. }