cowboy_http_websocket_handler.erl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.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 four callbacks: <em>websocket_init/3</em>,
  17. %% <em>websocket_handle/3</em>, <em>websocket_info/3</em> and
  18. %% <em>websocket_terminate/3</em>. These callbacks will only be called if the
  19. %% connection is upgraded to WebSocket in the HTTP handler's <em>init/3</em>
  20. %% callback. They are then called in that order, although
  21. %% <em>websocket_handle/3</em> will be called for each packet received,
  22. %% and <em>websocket_info</em> for each message received.
  23. %%
  24. %% <em>websocket_init/3</em> is meant for initialization. It receives
  25. %% information about the transport and protocol used, along with the handler
  26. %% options from the dispatch list. You can define a request-wide state here.
  27. %% If you are going to want to compact the request, you should probably do it
  28. %% here.
  29. %%
  30. %% <em>websocket_handle/3</em> receives the data from the socket. It can reply
  31. %% something, do nothing or close the connection.
  32. %%
  33. %% <em>websocket_info/3</em> receives messages sent to the process. It has
  34. %% the same reply format as <em>websocket_handle/3</em> described above. Note
  35. %% that unlike in a <em>gen_server</em>, when <em>websocket_info/3</em>
  36. %% replies something, it is always to the socket, not to the process that
  37. %% originated the message.
  38. %%
  39. %% <em>websocket_terminate/3</em> is meant for cleaning up. It also receives
  40. %% the request and the state previously defined, along with a reason for
  41. %% termination.
  42. %%
  43. %% All of <em>websocket_init/3</em>, <em>websocket_handle/3</em> and
  44. %% <em>websocket_info/3</em> can decide to hibernate the process by adding
  45. %% an extra element to the returned tuple, containing the atom
  46. %% <em>hibernate</em>. Doing so helps save memory and improve CPU usage.
  47. -module(cowboy_http_websocket_handler).
  48. -export([behaviour_info/1]).
  49. %% @private
  50. -spec behaviour_info(_)
  51. -> undefined | [{websocket_handle, 3} | {websocket_info, 3}
  52. | {websocket_init, 3} | {websocket_terminate, 3}, ...].
  53. behaviour_info(callbacks) ->
  54. [{websocket_init, 3}, {websocket_handle, 3},
  55. {websocket_info, 3}, {websocket_terminate, 3}];
  56. behaviour_info(_Other) ->
  57. undefined.