25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-26 17:38:45 +01:00

Use binary framing in MQTT WebSockets

This commit is contained in:
Evgeny Khramtsov 2019-04-25 14:30:42 +03:00
parent 5819733de6
commit a0c8c70c9c
3 changed files with 12 additions and 8 deletions

View File

@ -201,15 +201,15 @@ handle_sync_event({send_xml, Packet}, _From, StateName,
case Packet2 of case Packet2 of
{xmlstreamstart, Name, Attrs3} -> {xmlstreamstart, Name, Attrs3} ->
B = fxml:element_to_binary(#xmlel{name = Name, attrs = Attrs3}), B = fxml:element_to_binary(#xmlel{name = Name, attrs = Attrs3}),
WsPid ! {send, <<(binary:part(B, 0, byte_size(B)-2))/binary, ">">>}; WsPid ! {text, <<(binary:part(B, 0, byte_size(B)-2))/binary, ">">>};
{xmlstreamend, Name} -> {xmlstreamend, Name} ->
WsPid ! {send, <<"</", Name/binary, ">">>}; WsPid ! {text, <<"</", Name/binary, ">">>};
{xmlstreamelement, El} -> {xmlstreamelement, El} ->
WsPid ! {send, fxml:element_to_binary(El)}; WsPid ! {text, fxml:element_to_binary(El)};
{xmlstreamraw, Bin} -> {xmlstreamraw, Bin} ->
WsPid ! {send, Bin}; WsPid ! {text, Bin};
{xmlstreamcdata, Bin2} -> {xmlstreamcdata, Bin2} ->
WsPid ! {send, Bin2}; WsPid ! {text, Bin2};
skip -> skip ->
ok ok
end, end,
@ -224,7 +224,7 @@ handle_sync_event(close, _From, StateName, #state{ws = {_, WsPid}, rfc_compilant
when StateName /= stream_end_sent -> when StateName /= stream_end_sent ->
Close = #xmlel{name = <<"close">>, Close = #xmlel{name = <<"close">>,
attrs = [{<<"xmlns">>, <<"urn:ietf:params:xml:ns:xmpp-framing">>}]}, attrs = [{<<"xmlns">>, <<"urn:ietf:params:xml:ns:xmpp-framing">>}]},
WsPid ! {send, fxml:element_to_binary(Close)}, WsPid ! {text, fxml:element_to_binary(Close)},
{stop, normal, StateData}; {stop, normal, StateData};
handle_sync_event(close, _From, _StateName, StateData) -> handle_sync_event(close, _From, _StateName, StateData) ->
{stop, normal, StateData}. {stop, normal, StateData}.

View File

@ -202,10 +202,14 @@ ws_loop(FrameInfo, Socket, WsHandleLoopPid, SocketMode) ->
end, end,
erlang:demonitor(Ref), erlang:demonitor(Ref),
websocket_close(Socket, WsHandleLoopPid, SocketMode, Code); websocket_close(Socket, WsHandleLoopPid, SocketMode, Code);
{send, Data} -> {text, Data} ->
SocketMode:send(Socket, encode_frame(Data, 1)), SocketMode:send(Socket, encode_frame(Data, 1)),
ws_loop(FrameInfo, Socket, WsHandleLoopPid, ws_loop(FrameInfo, Socket, WsHandleLoopPid,
SocketMode); SocketMode);
{data, Data} ->
SocketMode:send(Socket, encode_frame(Data, 2)),
ws_loop(FrameInfo, Socket, WsHandleLoopPid,
SocketMode);
{ping, Data} -> {ping, Data} ->
SocketMode:send(Socket, encode_frame(Data, 9)), SocketMode:send(Socket, encode_frame(Data, 9)),
ws_loop(FrameInfo, Socket, WsHandleLoopPid, ws_loop(FrameInfo, Socket, WsHandleLoopPid,

View File

@ -98,7 +98,7 @@ init([{#ws{ip = IP, http_opts = ListenOpts}, WsPid}]) ->
end. end.
handle_call({send, Data}, _From, #state{ws_pid = WsPid} = State) -> handle_call({send, Data}, _From, #state{ws_pid = WsPid} = State) ->
WsPid ! {send, Data}, WsPid ! {data, Data},
{reply, ok, State}; {reply, ok, State};
handle_call(Request, From, State) -> handle_call(Request, From, State) ->
?WARNING_MSG("Got unexpected call from ~p: ~p", [From, Request]), ?WARNING_MSG("Got unexpected call from ~p: ~p", [From, Request]),