diff --git a/priv/tester.html b/priv/tester.html index 0b9e657..8861d65 100644 --- a/priv/tester.html +++ b/priv/tester.html @@ -76,6 +76,7 @@

nrte example

eventSource.onmessage = function (event) { showMessage("EventSource received: " + event.data); + updateMessageCount(); }; eventSource.onerror = function (event) { @@ -86,14 +87,11 @@

nrte example

function connectWebSocket() { disconnect(); - const uri = `ws://localhost:2080/websocket`; + const uri = `ws://localhost:2080/websocket?topics=topic1;topic2;topic3`; webSocket = new WebSocket(uri); - messageCount = 0; - webSocket.onopen = function(event) { showMessage("WebSocket connected"); - webSocket.send("topics: topic1;topic2;topic4"); }; webSocket.onmessage = function(event) { @@ -117,6 +115,9 @@

nrte example

eventSource = null; showMessage("EventSource disconnected"); } + + messageCount = -1; + updateMessageCount(); } function showMessage(message) { diff --git a/rebar.lock b/rebar.lock new file mode 100644 index 0000000..b6d0c4d --- /dev/null +++ b/rebar.lock @@ -0,0 +1,28 @@ +{"1.2.0", +[{<<"cowboy">>, + {git,"https://github.com/ninenines/cowboy.git", + {ref,"9f580ea964c4ea585aeeeb9f29613bf027d44269"}}, + 0}, + {<<"cowlib">>, + {git,"https://github.com/ninenines/cowlib", + {ref,"aca0ad953417b29bab2c41eeb4c37c98606c848b"}}, + 1}, + {<<"ebus">>,{pkg,<<"erlbus">>,<<"0.3.0">>},0}, + {<<"njson">>, + {git,"https://github.com/nomasystems/njson.git", + {ref,"541754472e8d9713fe8a320a7007c15015095c45"}}, + 0}, + {<<"nuid">>, + {git,"https://github.com/nomasystems/nuid.git", + {ref,"524ed2d7f7afb9860abe836cb9a852fa800a0aed"}}, + 0}, + {<<"ranch">>, + {git,"https://github.com/ninenines/ranch", + {ref,"10b51304b26062e0dbfd5e74824324e9a911e269"}}, + 1}]}. +[ +{pkg_hash,[ + {<<"ebus">>, <<"ECD5418FF2A365A9E809F553B15B624522DAD788C49F3AEDB64017C192568810">>}]}, +{pkg_hash_ext,[ + {<<"ebus">>, <<"45938BF8B97E26B02FE44B91D52BE333A2A82CF4220EBFEACC32588B2C84FA30">>}]} +]. diff --git a/src/nrte.erl b/src/nrte.erl index e12b5b1..dc8debf 100644 --- a/src/nrte.erl +++ b/src/nrte.erl @@ -26,7 +26,7 @@ start(_, _) -> BaseRoutes = [ {"/eventsource", nrte_eventsource, []}, - {"/websocket", nrte_websocket, []}, + {"/websocket", nrte_websocket, #{}}, {"/[...]", nrte_rest, []} ], Routes = diff --git a/src/nrte_websocket.erl b/src/nrte_websocket.erl index 0fec675..98c998c 100644 --- a/src/nrte_websocket.erl +++ b/src/nrte_websocket.erl @@ -15,7 +15,7 @@ -behaviour(cowboy_websocket). %%% COWBOY WEBSOCKET EXPORTS --export([init/2, websocket_handle/2, websocket_info/2]). +-export([init/2, websocket_init/1, websocket_handle/2, websocket_info/2]). %%%----------------------------------------------------------------------------- %%% COWBOY WEBSOCKET EXPORTS @@ -23,13 +23,16 @@ init(Req, Opts) -> case nrte_auth:authorization(Req, subscribe) of {authorized, TopicList} -> - nrte:subscribe(TopicList), - nrte_publications:subscription_init_link_terminate(<<"websocket">>, TopicList), - {cowboy_websocket, Req, Opts}; + {cowboy_websocket, Req, Opts#{topic_list => TopicList}}; {unauthorized, Req2} -> {stop, Req2, Opts} end. +websocket_init(#{topic_list := TopicList} = State) -> + nrte:subscribe(TopicList), + nrte_publications:subscription_init_link_terminate(<<"websocket">>, TopicList), + {ok, State}. + websocket_handle(_Data, State) -> {ok, State}.