Packages

WebSocket protocol library for Erlang (RFC 6455 / RFC 8441 / RFC 9220)

Current section

Files

Jump to
erlang_ws examples echo_client.erl
Raw

examples/echo_client.erl

%% @doc Example: synchronous echo client.
%%
%% Sends a user-supplied text message, waits for the server's echo,
%% closes the connection cleanly.
%%
%% echo_client:send(<<"ws://127.0.0.1:8080/">>, <<"hello">>).
-module(echo_client).
-behaviour(ws_handler).
-export([send/2, send/3]).
-export([init/2, handle_in/2, handle_info/2, terminate/2]).
-spec send(binary(), binary()) -> {ok, binary()} | {error, term()}.
send(Url, Msg) -> send(Url, Msg, 5000).
-spec send(binary(), binary(), timeout()) -> {ok, binary()} | {error, term()}.
send(Url, Msg, Timeout) ->
{ok, _} = application:ensure_all_started(ws),
case ws:connect(Url,
#{handler => ?MODULE,
handler_opts => #{notify => self()}}) of
{ok, Conn} ->
ok = ws:send(Conn, {text, Msg}),
Result = receive
{echo, Bin} -> {ok, Bin}
after Timeout ->
{error, timeout}
end,
ok = ws:close(Conn, 1000, <<>>),
Result;
Err -> Err
end.
%% --- ws_handler callbacks --------------------------------------------
init(_Req, #{notify := Pid} = State) when is_pid(Pid) ->
{ok, State}.
handle_in({text, Data}, #{notify := Pid} = State) ->
Pid ! {echo, Data},
{ok, State};
handle_in(_, State) ->
{ok, State}.
handle_info(_Msg, State) ->
{ok, State}.
terminate(_Reason, _State) ->
ok.