cowboy_websocket_handler.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 terminate_reason() :: {normal, closed}
  52. | {normal, timeout}
  53. | {error, closed}
  54. | {error, badframe}
  55. | {error, atom()}.
  56. -callback websocket_init(atom(), Req, opts())
  57. -> {ok, Req, state()}
  58. | {ok, Req, state(), hibernate}
  59. | {ok, Req, state(), timeout()}
  60. | {ok, Req, state(), timeout(), hibernate}
  61. | {shutdown, Req}
  62. when Req::cowboy_req:req().
  63. -callback websocket_handle({text | binary | ping | pong, binary()}, Req, State)
  64. -> {ok, Req, State}
  65. | {ok, Req, State, hibernate}
  66. | {reply, {text | binary | ping | pong, binary()}, Req, State}
  67. | {reply, {text | binary | ping | pong, binary()}, Req, State, hibernate}
  68. | {shutdown, Req, State}
  69. when Req::cowboy_req:req(), State::state().
  70. -callback websocket_info(any(), Req, State)
  71. -> {ok, Req, State}
  72. | {ok, Req, State, hibernate}
  73. | {reply, {text | binary | ping | pong, binary()}, Req, State}
  74. | {reply, {text | binary | ping | pong, binary()}, Req, State, hibernate}
  75. | {shutdown, Req, State}
  76. when Req::cowboy_req:req(), State::state().
  77. -callback websocket_terminate(terminate_reason(), cowboy_req:req(), state())
  78. -> ok.