25
1
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:
Eric Cestari 2010-09-17 16:10:59 +02:00
parent b44c462b0e
commit 8ea523889b
2 changed files with 15 additions and 17 deletions

View File

@ -151,12 +151,12 @@ handle_event({activate, From}, StateName, StateData) ->
handle_sync_event({send, Packet}, _From, StateName, #state{ws = WS} = StateData) -> handle_sync_event({send, Packet}, _From, StateName, #state{ws = WS} = StateData) ->
Packet2 = if Packet2 = if
is_binary(Packet) -> is_binary(Packet) ->
binary_to_list(Packet); Packet;
true -> true ->
Packet list_to_binary(Packet)
end, end,
?DEBUG("sending on websocket : ~p ", [Packet2]), ?DEBUG("sending on websocket : ~p ", [Packet2]),
WS:send(lists:flatten(Packet2)), WS:send(Packet2),
{reply, ok, StateName, StateData}; {reply, ok, StateName, StateData};
handle_sync_event(close, _From, _StateName, StateData) -> handle_sync_event(close, _From, _StateName, StateData) ->
@ -169,8 +169,7 @@ handle_info({browser, Packet}, StateName, StateData)->
Input = [StateData#state.input|Packet], Input = [StateData#state.input|Packet],
StateData#state{input = Input}; StateData#state{input = Input};
{Receiver, _Tag} -> {Receiver, _Tag} ->
Receiver ! {tcp, StateData#state.socket, Receiver ! {tcp, StateData#state.socket,Packet},
list_to_binary(Packet)},
cancel_timer(StateData#state.timer), cancel_timer(StateData#state.timer),
Timer = erlang:start_timer(StateData#state.timeout, self(), []), Timer = erlang:start_timer(StateData#state.timeout, self(), []),
StateData#state{waiting_input = false, StateData#state{waiting_input = false,

View File

@ -212,10 +212,9 @@ build_challenge({'draft-hixie', 0}, {Key1, Key2, Key3}) ->
ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit) -> ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit) ->
%?DEBUG("websocket loop", []),
receive receive
{tcp, Socket, Data} -> {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} -> {tcp_closed, Socket} ->
?DEBUG("tcp connection was closed, exit", []), ?DEBUG("tcp connection was closed, exit", []),
% close websocket and custom controlling loop % close websocket and custom controlling loop
@ -233,8 +232,8 @@ ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit) ->
% close websocket and custom controlling loop % close websocket and custom controlling loop
websocket_close(Socket, WsHandleLoopPid, SocketMode, WsAutoExit); websocket_close(Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
{send, Data} -> {send, Data} ->
%?DEBUG("sending data to websocket: ~p", [Data]), ?DEBUG("sending data to websocket: ~p", [Data]),
SocketMode:send(Socket, [0, Data, 255]), SocketMode:send(Socket, iolist_to_binary([0,Data,255])),
ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit); ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit);
shutdown -> shutdown ->
?DEBUG("shutdown request received, closing websocket with pid ~p", [self()]), ?DEBUG("shutdown request received, closing websocket with pid ~p", [self()]),
@ -246,20 +245,20 @@ ws_loop(Socket, Buffer, WsHandleLoopPid, SocketMode, WsAutoExit) ->
end. end.
% Buffering and data handling % Buffering and data handling
handle_data(none, [0|T], Socket, WsHandleLoopPid, SocketMode, WsAutoExit) -> handle_data(none,<<0,T/binary>>, Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
handle_data([], T, 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); ws_loop(Socket, none, WsHandleLoopPid, SocketMode, WsAutoExit);
handle_data(L, [255|T], Socket, WsHandleLoopPid, SocketMode, WsAutoExit) -> handle_data(L, <<255,T/binary>>, Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
WsHandleLoopPid ! {browser, lists:reverse(L)}, WsHandleLoopPid ! {browser, iolist_to_binary(L)},
handle_data(none, T, Socket, WsHandleLoopPid, SocketMode, WsAutoExit); handle_data(none, T, Socket, WsHandleLoopPid, SocketMode, WsAutoExit);
handle_data(L, [H|T], Socket, WsHandleLoopPid, SocketMode, WsAutoExit) -> handle_data(L, <<H/utf8,T/binary>>, Socket, WsHandleLoopPid, SocketMode, WsAutoExit) ->
handle_data([H|L], T, 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). ws_loop(Socket, L, WsHandleLoopPid, SocketMode, WsAutoExit).
% Close socket and custom handling loop dependency % Close socket and custom handling loop dependency