Permalink
Browse files

Rewrote message parser to supported buffered messages

  • Loading branch information...
omarkj committed Jan 27, 2011
1 parent 94ee31c commit 5d1e9b4a9c2978dabf6bec50dd47dc0d2acf5855
Showing with 37 additions and 23 deletions.
  1. +7 −2 src/socketio.erl
  2. +17 −13 src/socketio_utils.erl
  3. +5 −3 src/socketio_ws.erl
  4. +8 −5 src/socketio_xhrpolling.erl
View
@@ -55,8 +55,13 @@ create(<<"xhr-polling/", Rest/binary>>, Req, AutoExit, Options, Loop) ->
Pid ->
case Tail of
[<<"send">>] ->
- <<"data=", Data/binary>> = Req:recv_body(),
- gen_server:cast(Pid, {data, Req, Data});
+ Incoming = case Req:recv_body() of
+ <<"data=", Data/binary>> ->
+ Data;
+ _ ->
+ <<>>
+ end,
+ gen_server:cast(Pid, {data, Req, Incoming});
_ ->
gen_server:cast(Pid, {poll, Req})
end
View
@@ -1,6 +1,6 @@
-module (socketio_utils).
--export ([encode/1, decode/1, get_heartbeat/1, ref_to_msg/1, random/0]).
+-export ([encode/1, decode/2, get_heartbeat/1, ref_to_msg/1, random/0]).
% Encode messages to Socket.IO format
encode({json, Message}) ->
@@ -12,18 +12,22 @@ encode(Message) ->
% Messages look like:
% ~m~MSGLENGTH~m~MESSAGE or
% ~m~MSGLENGTH~m~~j~JSON
-decode(<<"\~m\~", Rest/binary>>) ->
- case binary:match(Rest, <<"\~m\~">>) of
- nomatch -> badmsg; % Not valid, not sure what to do.
- {Beginning, End} ->
- Cons = Beginning + End,
- M = erlang:size(Rest) - Cons,
- binary:part(Rest, {Cons, M});
- _ ->
- badmsg
- end;
-decode(Message) ->
- Message.
+% TODO: REWRITE FOR BUFFERED MSGS
+decode(<<"\~m\~", Message/binary>>, Buffer) ->
+ [_|[Message0]] = binary:split(Message, <<"\~m\~">>), % Get past the length
+ case binary:split(Message, <<"\~m\~">>) of
+ [_|[Message0]] ->
+ case binary:match(Message0, <<"\~m\~">>) of
+ {Beginning, _} ->
+ <<Message1:Beginning/binary, Rest/binary>> = Message0,
+ NewBuffer = Buffer ++ [Message1],
+ decode(Rest, NewBuffer);
+ nomatch ->
+ Buffer ++ [Message0]
+ end
+ end;
+decode(_, Buffer) ->
+ Buffer.
get_heartbeat(Number) ->
binary:list_to_bin([<<"\~h\~">>, integer_to_list(Number)]).
View
@@ -71,11 +71,13 @@ init([Req, Version, AutoExit, {Timeout}, Loop]) ->
monitor(process, SocketPid),
{ok, {SocketIo, SocketIoLoop, SocketPid}}.
-handle_cast({data, Buffer}, {SocketIo, SocketIoLoop, SocketPid}) ->
- case socketio_utils:decode(Buffer) of
+handle_cast({data, Data}, {SocketIo, SocketIoLoop, SocketPid}) ->
+ case socketio_utils:decode(Data, []) of
heartbeat -> void;
Message ->
- SocketIoLoop ! {data, Message}
+ lists:map(fun(M) ->
+ SocketIoLoop ! {data, M}
+ end, Message)
end,
{noreply, {SocketIo, SocketIoLoop, SocketPid}};
@@ -90,11 +90,16 @@ handle_cast({poll, Req}, State = #state{timeout = Timeout}) ->
{noreply, NewState};
handle_cast({data, Req, Body}, State = #state{socketioloop = SocketIoLoop}) ->
- case socketio_utils:decode(Body) of
+ case socketio_utils:decode(Body, []) of
heartbeat -> void;
Message ->
- [{Data, _}|_] = mochiweb_util:parse_qs(Message), % Getting this encoded
- SocketIoLoop ! {data, list_to_binary(Data)}
+ lists:map(fun(M) ->
+ case mochiweb_util:parse_qs(M) of
+ [{Data, _}|_] ->
+ SocketIoLoop ! {data, list_to_binary(Data)};
+ _ -> void
+ end
+ end, Message)
end,
Req:ok({_ContentType = "text/plain",
_Headers = [{"Access-Control-Allow-Origin", "*"},
@@ -116,7 +121,6 @@ handle_cast(flush, State = #state{buffer = Buffer, xhrpid = XhrPid}) ->
NewState = case length(Buffer) of
0 -> State;
_ ->
- io:format("Flush the buffer of length ~p~n", [length(Buffer)]),
XhrPid ! {send, Buffer},
State#state{buffer = []}
end,
@@ -155,7 +159,6 @@ handle_call(_Request, _From, State) ->
{reply, Reply, State}.
xhr_loop(Req, XhrPollingPid, Timeout) ->
- io:format("In loop~n"),
receive
{send, Message} ->
gen_server:cast(XhrPollingPid, gone),

0 comments on commit 5d1e9b4

Please sign in to comment.