Skip to content

Commit

Permalink
Add AsyncStart optional parameter to start_link
Browse files Browse the repository at this point in the history
  • Loading branch information
maximvl authored and jeremyong committed Apr 13, 2014
1 parent 4d5576c commit f0334f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/websocket_client.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{application, websocket_client,
[
{description, "Erlang websocket client"},
{vsn, "0.5.4"},
{vsn, "0.6.1"},
{registered, []},
{applications, [
ssl,
Expand Down
15 changes: 10 additions & 5 deletions src/websocket_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

-export([
start_link/3,
start_link/4,
cast/2,
send/2
]).

-export([ws_client_init/6]).
-export([ws_client_init/7]).

%% @doc Start the websocket client
-spec start_link(URL :: string(), Handler :: module(), Args :: list()) ->
{ok, pid()} | {error, term()}.
start_link(URL, Handler, Args) ->
start_link(URL, Handler, Args, true).

start_link(URL, Handler, Args, AsyncStart) ->
case http_uri:parse(URL, [{scheme_defaults, [{ws,80},{wss,443}]}]) of
{ok, {Protocol, _, Host, Port, Path, Query}} ->
proc_lib:start_link(?MODULE, ws_client_init,
[Handler, Protocol, Host, Port, Path ++ Query, Args]);
[Handler, Protocol, Host, Port, Path ++ Query, Args, AsyncStart]);
{error, _} = Error ->
Error
end.
Expand All @@ -32,9 +36,9 @@ cast(Client, Frame) ->
%% @doc Create socket, execute handshake, and enter loop
-spec ws_client_init(Handler :: module(), Protocol :: websocket_req:protocol(),
Host :: string(), Port :: inet:port_number(), Path :: string(),
Args :: list()) ->
Args :: list(), AsyncStart :: boolean()) ->
no_return().
ws_client_init(Handler, Protocol, Host, Port, Path, Args) ->
ws_client_init(Handler, Protocol, Host, Port, Path, Args, AsyncStart) ->
Transport = case Protocol of
wss ->
ssl;
Expand Down Expand Up @@ -77,13 +81,14 @@ ws_client_init(Handler, Protocol, Host, Port, Path, Args) ->
proc_lib:init_ack(HandshakeError),
exit(normal);
{ok, Buffer} ->
proc_lib:init_ack({ok, self()}),
AsyncStart andalso proc_lib:init_ack({ok, self()}),
{ok, HandlerState, KeepAlive} = case Handler:init(Args, WSReq) of
{ok, HS} ->
{ok, HS, infinity};
{ok, HS, KA} ->
{ok, HS, KA}
end,
AsyncStart orelse proc_lib:init_ack({ok, self()}),
case Socket of
{sslsocket, _, _} ->
ssl:setopts(Socket, [{active, true}]);
Expand Down

0 comments on commit f0334f0

Please sign in to comment.