mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-24 17:29:28 +01:00
[TECH-1151] Websockets are now handled in pure binary
This commit is contained in:
parent
b44c462b0e
commit
8ea523889b
@ -151,12 +151,12 @@ handle_event({activate, From}, StateName, StateData) ->
|
||||
handle_sync_event({send, Packet}, _From, StateName, #state{ws = WS} = StateData) ->
|
||||
Packet2 = if
|
||||
is_binary(Packet) ->
|
||||
binary_to_list(Packet);
|
||||
Packet;
|
||||
true ->
|
||||
Packet
|
||||
list_to_binary(Packet)
|
||||
end,
|
||||
?DEBUG("sending on websocket : ~p ", [Packet2]),
|
||||
WS:send(lists:flatten(Packet2)),
|
||||
WS:send(Packet2),
|
||||
{reply, ok, StateName, StateData};
|
||||
|
||||
handle_sync_event(close, _From, _StateName, StateData) ->
|
||||
@ -169,8 +169,7 @@ handle_info({browser, Packet}, StateName, StateData)->
|
||||
Input = [StateData#state.input|Packet],
|
||||
StateData#state{input = Input};
|
||||
{Receiver, _Tag} ->
|
||||
Receiver ! {tcp, StateData#state.socket,
|
||||
list_to_binary(Packet)},
|
||||
Receiver ! {tcp, StateData#state.socket,Packet},
|
||||
cancel_timer(StateData#state.timer),
|
||||
Timer = erlang:start_timer(StateData#state.timeout, self(), []),
|
||||
StateData#state{waiting_input = false,
|
||||
|
@ -212,10 +212,9 @@ build_challenge({'draft-hixie', 0}, {Key1, Key2, Key3}) ->
|
||||
|
||||
|
||||
ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
%?DEBUG("websocket loop", []),
|
||||
receive
|
||||
{tcp, Socket, Data} ->
|
||||
handle_data(Buffer, binary_to_list(Data), Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
handle_data(Buffer, Data, Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
{tcp_closed, Socket} ->
|
||||
?DEBUG("tcp connection was closed, exit", []),
|
||||
% close websocket and custom controlling loop
|
||||
@ -233,8 +232,8 @@ ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
% close websocket and custom controlling loop
|
||||
websocket_close(Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
{send, Data} ->
|
||||
%?DEBUG("sending data to websocket: ~p", [Data]),
|
||||
SocketMode:send(Socket, [0, Data, 255]),
|
||||
?DEBUG("sending data to websocket: ~p", [Data]),
|
||||
SocketMode:send(Socket, iolist_to_binary([0,Data,255])),
|
||||
ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
shutdown ->
|
||||
?DEBUG("shutdown request received, closing websocket with pid ~p", [self()]),
|
||||
@ -246,20 +245,20 @@ ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
end.
|
||||
|
||||
% Buffering and data handling
|
||||
handle_data(none, [0|T], Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
handle_data([], T, Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
handle_data(none,<<0,T/binary>>, Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
handle_data(<<>>, T, Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
|
||||
handle_data(none, [], Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
handle_data(none, <<>>, Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
ws_loop(Socket, none, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
|
||||
handle_data(L, [255|T], Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
WsHandleLoopPid ! {browser, lists:reverse(L)},
|
||||
handle_data(L, <<255,T/binary>>, Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
WsHandleLoopPid ! {browser, iolist_to_binary(L)},
|
||||
handle_data(none, T, Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
|
||||
handle_data(L, [H|T], Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
handle_data([H|L], T, Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
handle_data(L, <<H/utf8,T/binary>>, Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
handle_data(iolist_to_binary([L, H]), T, Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
|
||||
|
||||
handle_data([], L, Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
handle_data(<<>>, L, Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
|
||||
ws_loop(Socket, L, WsHandleLoopPid, SocketMode, WsAutoExit).
|
||||
|
||||
% Close socket and custom handling loop dependency
|
||||
|
Loading…
Reference in New Issue
Block a user