cowboy_websocket_handler.erl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. %% Copyright (c) 2011-2014, 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. -module(cowboy_websocket_handler).
  15. -type opts() :: any().
  16. -type state() :: any().
  17. -type terminate_reason() :: {normal, shutdown}
  18. | {normal, timeout}
  19. | {error, closed}
  20. | {remote, closed}
  21. | {remote, cowboy_websocket:close_code(), binary()}
  22. | {error, badencoding}
  23. | {error, badframe}
  24. | {error, atom()}.
  25. -callback websocket_init(atom(), Req, opts())
  26. -> {ok, Req, state()}
  27. | {ok, Req, state(), hibernate}
  28. | {ok, Req, state(), timeout()}
  29. | {ok, Req, state(), timeout(), hibernate}
  30. | {shutdown, Req}
  31. when Req::cowboy_req:req().
  32. -callback websocket_handle({text | binary | ping | pong, binary()}, Req, State)
  33. -> {ok, Req, State}
  34. | {ok, Req, State, hibernate}
  35. | {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State}
  36. | {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State, hibernate}
  37. | {shutdown, Req, State}
  38. when Req::cowboy_req:req(), State::state().
  39. -callback websocket_info(any(), Req, State)
  40. -> {ok, Req, State}
  41. | {ok, Req, State, hibernate}
  42. | {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State}
  43. | {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State, hibernate}
  44. | {shutdown, Req, State}
  45. when Req::cowboy_req:req(), State::state().
  46. -callback websocket_terminate(terminate_reason(), cowboy_req:req(), state())
  47. -> ok.