cowboy_websocket_handler.erl 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. %% Copyright (c) 2011-2012, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. %% @doc Handler for HTTP WebSocket requests.
  15. %%
  16. %% WebSocket handlers must implement five callbacks: <em>init/3</em>,
  17. %% <em>websocket_init/3</em>, <em>websocket_handle/3</em>,
  18. %% <em>websocket_info/3</em> and <em>websocket_terminate/3</em>.
  19. %% These callbacks will only be called if the connection is upgraded
  20. %% to WebSocket in the HTTP handler's <em>init/3</em> callback.
  21. %% They are then called in that order, although <em>websocket_handle/3</em>
  22. %% will be called for each packet received, and <em>websocket_info</em>
  23. %% for each message received.
  24. %%
  25. %% <em>websocket_init/3</em> is meant for initialization. It receives
  26. %% information about the transport and protocol used, along with the handler
  27. %% options from the dispatch list. You can define a request-wide state here.
  28. %% If you are going to want to compact the request, you should probably do it
  29. %% here.
  30. %%
  31. %% <em>websocket_handle/3</em> receives the data from the socket. It can reply
  32. %% something, do nothing or close the connection.
  33. %%
  34. %% <em>websocket_info/3</em> receives messages sent to the process. It has
  35. %% the same reply format as <em>websocket_handle/3</em> described above. Note
  36. %% that unlike in a <em>gen_server</em>, when <em>websocket_info/3</em>
  37. %% replies something, it is always to the socket, not to the process that
  38. %% originated the message.
  39. %%
  40. %% <em>websocket_terminate/3</em> is meant for cleaning up. It also receives
  41. %% the request and the state previously defined, along with a reason for
  42. %% termination.
  43. %%
  44. %% All of <em>websocket_init/3</em>, <em>websocket_handle/3</em> and
  45. %% <em>websocket_info/3</em> can decide to hibernate the process by adding
  46. %% an extra element to the returned tuple, containing the atom
  47. %% <em>hibernate</em>. Doing so helps save memory and improve CPU usage.
  48. -module(cowboy_websocket_handler).
  49. -type opts() :: any().
  50. -type state() :: any().
  51. -type payload() :: {text | binary | ping | pong, binary()}.
  52. -type terminate_reason() :: {normal, closed}
  53. | {normal, timeout}
  54. | {error, closed}
  55. | {error, badframe}
  56. | {error, atom()}.
  57. -callback websocket_init(atom(), Req, opts())
  58. -> {ok, Req, state()}
  59. | {ok, Req, state(), hibernate}
  60. | {ok, Req, state(), timeout()}
  61. | {ok, Req, state(), timeout(), hibernate}
  62. | {shutdown, Req}
  63. when Req::cowboy_req:req().
  64. -callback websocket_handle({text | binary | ping | pong, binary()}, Req, State)
  65. -> {ok, Req, State}
  66. | {ok, Req, State, hibernate}
  67. | {reply, payload() | [payload()], Req, State}
  68. | {reply, payload() | [payload()], Req, State, hibernate}
  69. | {shutdown, Req, State}
  70. when Req::cowboy_req:req(), State::state().
  71. -callback websocket_info(any(), Req, State)
  72. -> {ok, Req, State}
  73. | {ok, Req, State, hibernate}
  74. | {reply, payload() | [payload()], Req, State}
  75. | {reply, payload() | [payload()], Req, State, hibernate}
  76. | {shutdown, Req, State}
  77. when Req::cowboy_req:req(), State::state().
  78. -callback websocket_terminate(terminate_reason(), cowboy_req:req(), state())
  79. -> ok.