cowboy_websocket.ezdoc 4.2 KB

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