Skip to content

Commit f8dac60

Browse files
author
Simon MacMullen
committed
Send closed event to the app with HTTP transports.
1 parent fb5e2dd commit f8dac60

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/sockjs_session.erl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,30 @@
33
-behaviour(sockjs_sender).
44
-behaviour(gen_server).
55

6-
-export([init/0, start_link/1, maybe_create/2, sender/1, reply/2]).
6+
-export([init/0, start_link/2, maybe_create/2, sender/1, reply/2]).
77

88
-export([send/2, close/3]).
99

1010
-export([init/1, handle_call/3, handle_info/2, terminate/2, code_change/3,
1111
handle_cast/2]).
1212

13-
-record(session, {id, outbound_queue = queue:new(), response_pid,
13+
-record(session, {id, outbound_queue = queue:new(), response_pid, receiver,
1414
session_timeout, closed = false, close_msg}).
1515
-define(ETS, sockjs_table).
1616

1717
init() ->
1818
ets:new(?ETS, [public, named_table]).
1919

20-
start_link(SessionId) ->
21-
gen_server:start_link(?MODULE, SessionId, []).
20+
start_link(SessionId, Receive) ->
21+
gen_server:start_link(?MODULE, {SessionId, Receive}, []).
2222

2323
maybe_create(dummy, _) ->
2424
ok;
2525

2626
maybe_create(SessionId, Receive) ->
2727
case ets:lookup(?ETS, SessionId) of
28-
[] -> {ok, SPid} = sockjs_session_sup:start_child(SessionId),
28+
[] -> {ok, SPid} = sockjs_session_sup:start_child(
29+
SessionId, Receive),
2930
enqueue({open, nil}, SessionId),
3031
Receive({?MODULE, SessionId}, init),
3132
SPid;
@@ -90,10 +91,10 @@ reply(Reply, Pid, State = #session{response_pid = Pid}) ->
9091

9192
%% --------------------------------------------------------------------------
9293

93-
init(SessionId) ->
94+
init({SessionId, Receive}) ->
9495
ets:insert(?ETS, {SessionId, self()}),
9596
process_flag(trap_exit, true),
96-
{ok, #session{id = SessionId}}.
97+
{ok, #session{id = SessionId, receiver = Receive}}.
9798

9899
%% For non-streaming transports we want to send a closed message every time
99100
%% we are asked - for streaming transports we only want to send it once.
@@ -141,8 +142,10 @@ handle_info(session_timeout, State = #session{response_pid = undefined}) ->
141142
handle_info(Info, State) ->
142143
{stop, {odd_info, Info}, State}.
143144

144-
terminate(_Reason, #session{id = ID}) ->
145-
ets:delete(?ETS, ID),
145+
terminate(_Reason, #session{id = SessionId,
146+
receiver = Receive}) ->
147+
Receive({?MODULE, SessionId}, closed),
148+
ets:delete(?ETS, SessionId),
146149
ok.
147150

148151
code_change(_OldVsn, State, _Extra) ->

src/sockjs_session_sup.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
-behaviour(supervisor).
44

5-
-export([start_link/0, start_child/1]).
5+
-export([start_link/0, start_child/2]).
66
-export([init/1]).
77

88
start_link() ->
@@ -11,8 +11,8 @@ start_link() ->
1111
init([]) ->
1212
{ok, {{one_for_one, 10, 10}, []}}.
1313

14-
start_child(SessionId) ->
14+
start_child(SessionId, Receive) ->
1515
supervisor:start_child(
1616
?MODULE,
17-
{SessionId, {sockjs_session, start_link, [SessionId]},
17+
{SessionId, {sockjs_session, start_link, [SessionId, Receive]},
1818
transient, 16#ffffffff, worker, [sockjs_session]}).

0 commit comments

Comments
 (0)