cowboy_websocket.asciidoc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. = cowboy_websocket(3)
  2. == Name
  3. cowboy_websocket - Websocket protocol
  4. == Description
  5. The `cowboy_websocket` module implements the Websocket protocol.
  6. This module is a sub protocol that defines four callbacks to
  7. be implemented by handlers. The `init/2` and `terminate/3`
  8. callbacks are common to all handler types and are documented
  9. in the manual for the link:cowboy_handler.asciidoc[cowboy_handler] module.
  10. The `websocket_handle/2` and `websocket_info/2` callbacks are
  11. specific to Websocket handlers and will be called as many times
  12. as necessary until the Websocket connection is closed.
  13. The `init/2` callback can be used to negotiate Websocket protocol
  14. extensions with the client. It is highly recommended to return a
  15. timeout value from this callback to ensure that the process is
  16. terminated when no data has been received during that timespan.
  17. The default timeout is `infinity`, which should only be used if
  18. you have alternate means of ending inactive connections.
  19. Cowboy will terminate the process right after closing the
  20. Websocket connection. This means that there is no real need to
  21. perform any cleanup in the optional `terminate/3` callback.
  22. == Meta values
  23. websocket_compress = boolean()::
  24. Whether a websocket compression extension in in use.
  25. websocket_version = 7 | 8 | 13::
  26. The version of the Websocket protocol being used.
  27. == Terminate reasons
  28. The following values may be received as the terminate reason
  29. in the optional `terminate/3` callback.
  30. normal::
  31. The connection was closed normally before establishing a Websocket
  32. connection. This typically happens if an `ok` tuple is returned
  33. from the `init/2` callback.
  34. remote::
  35. The remote endpoint closed the connection without giving any
  36. further details.
  37. {remote, Code, Payload}::
  38. The remote endpoint closed the connection with the given
  39. `Code` and `Payload` as the reason.
  40. stop::
  41. The handler requested to close the connection, either by returning
  42. a `stop` tuple or by sending a `close` frame.
  43. timeout::
  44. The connection has been closed due to inactivity. The timeout
  45. value can be configured from `init/2`.
  46. {crash, Class, Reason}::
  47. A crash occurred in the handler. `Class` and `Reason` can be
  48. used to obtain more information about the crash. The function
  49. `erlang:get_stacktrace/0` can also be called to obtain the
  50. stacktrace of the process when the crash occurred.
  51. {error, badencoding}::
  52. A text frame was sent by the client with invalid encoding. All
  53. text frames must be valid UTF-8.
  54. {error, badframe}::
  55. A protocol error has been detected.
  56. {error, closed}::
  57. The socket has been closed brutally without a close frame being
  58. received first.
  59. {error, Reason}::
  60. A socket error ocurred.
  61. == Callbacks
  62. === websocket_handle(InFrame, State) -> Ret
  63. [source,erlang]
  64. ----
  65. Ret = {ok, State}
  66. | {ok, State, hibernate}
  67. | {reply, OutFrame | [OutFrame], State}
  68. | {reply, OutFrame | [OutFrame], State, hibernate}
  69. | {stop, State}
  70. InFrame = {text | binary | ping | pong, binary()}
  71. State = any()
  72. OutFrame = cow_ws:frame()
  73. ----
  74. Handle the data received from the Websocket connection.
  75. This function will be called every time data is received
  76. from the Websocket connection.
  77. The `stop` return value can be used to close the
  78. connection. A close reply will also result in the connection
  79. being closed.
  80. The `hibernate` option will hibernate the process until
  81. it receives new data from the Websocket connection or an
  82. Erlang message.
  83. === websocket_info(Info, State) -> Ret
  84. [source,erlang]
  85. ----
  86. Ret = {ok, State}
  87. | {ok, State, hibernate}
  88. | {reply, OutFrame | [OutFrame], State}
  89. | {reply, OutFrame | [OutFrame], State, hibernate}
  90. | {stop, State}
  91. Info = any()
  92. State = any()
  93. OutFrame = cow_ws:frame()
  94. ----
  95. Handle the Erlang message received.
  96. This function will be called every time an Erlang message
  97. has been received. The message can be any Erlang term.
  98. The `stop` return value can be used to close the
  99. connection. A close reply will also result in the connection
  100. being closed.
  101. The `hibernate` option will hibernate the process until
  102. it receives another message or new data from the Websocket
  103. connection.