echo_protocol.erl 572 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/4]).
  5. -export([init/4]).
  6. start_link(Ref, Socket, Transport, Opts) ->
  7. Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
  8. {ok, Pid}.
  9. init(Ref, Socket, Transport, _Opts = []) ->
  10. ok = ranch:accept_ack(Ref),
  11. loop(Socket, Transport).
  12. loop(Socket, Transport) ->
  13. case Transport:recv(Socket, 0, 5000) of
  14. {ok, Data} ->
  15. Transport:send(Socket, Data),
  16. loop(Socket, Transport);
  17. _ ->
  18. ok = Transport:close(Socket)
  19. end.