echo_protocol.erl 578 B

123456789101112131415161718192021222324
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. -module(echo_protocol).
  3. -behaviour(ranch_protocol).
  4. -export([start_link/3]).
  5. -export([init/3]).
  6. start_link(Ref, Transport, Opts) ->
  7. Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
  8. {ok, Pid}.
  9. init(Ref, Transport, _Opts = []) ->
  10. {ok, Socket} = ranch:handshake(Ref),
  11. loop(Socket, Transport).
  12. loop(Socket, Transport) ->
  13. case Transport:recv(Socket, 0, 60000) of
  14. {ok, Data} when Data =/= <<4>> ->
  15. Transport:send(Socket, Data),
  16. loop(Socket, Transport);
  17. _ ->
  18. ok = Transport:close(Socket)
  19. end.