websocket_echo_handler.erl 890 B

12345678910111213141516171819202122232425262728293031323334
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. -module(websocket_echo_handler).
  3. -behaviour(cowboy_http_handler).
  4. -behaviour(cowboy_websocket_handler).
  5. -export([init/3, handle/2, terminate/2]).
  6. -export([websocket_init/3, websocket_handle/3,
  7. websocket_info/3, websocket_terminate/3]).
  8. init(_Any, _Req, _Opts) ->
  9. {upgrade, protocol, cowboy_websocket}.
  10. handle(_Req, _State) ->
  11. exit(badarg).
  12. terminate(_Req, _State) ->
  13. exit(badarg).
  14. websocket_init(_TransportName, Req, _Opts) ->
  15. Req2 = cowboy_req:compact(Req),
  16. {ok, Req2, undefined}.
  17. websocket_handle({text, Data}, Req, State) ->
  18. {reply, {text, Data}, Req, State};
  19. websocket_handle({binary, Data}, Req, State) ->
  20. {reply, {binary, Data}, Req, State};
  21. websocket_handle(_Frame, Req, State) ->
  22. {ok, Req, State}.
  23. websocket_info(_Info, Req, State) ->
  24. {ok, Req, State}.
  25. websocket_terminate(_Reason, _Req, _State) ->
  26. ok.