Browse Source

new protocol

Maxim Sokhatsky 11 years ago
parent
commit
29f99a692a
2 changed files with 16 additions and 6 deletions
  1. 1 1
      apps/face/priv/templates/index.html
  2. 15 5
      apps/face/src/n2o_game.erl

+ 1 - 1
apps/face/priv/templates/index.html

@@ -6,7 +6,7 @@
 <body>
 {{body}}
 <script>var transition = {pid: '', port:'8000'};</script>
-<script>function handle_web_socket(body) { console.log(body); } </script>
+<script>function handle_web_socket(body) { console.log(String(bert.decodebuf(body))); } </script>
 <script src='/static/nitrogen/jquery.js' type='text/javascript' charset='utf-8'></script>
 <script src='/static/nitrogen/bullet.js' type='text/javascript' charset='utf-8'></script>
 <script src='/static/nitrogen/n2o.js' type='text/javascript' charset='utf-8'></script>

+ 15 - 5
apps/face/src/n2o_game.erl

@@ -5,6 +5,7 @@
 -include("../../server/include/game_okey.hrl").
 -include("../../server/include/game_tavla.hrl").
 
+
 -export([init/4]).
 -export([stream/3]).
 -export([info/3]).
@@ -44,7 +45,8 @@ html_events(Pro, State) ->
     GenActions = get(actions),
     RenderGenActions = wf:render(GenActions),
     wf_context:clear_actions(),
-    [<<"EVAL">>,Render,RenderGenActions].
+    JS = iolist_to_binary([Render,RenderGenActions]),
+    wf:json([{eval,JS}]).
 
 stream(<<"ping">>, Req, State) ->
     wf:info("ping received~n"),
@@ -75,7 +77,14 @@ info({client,Message}, Req, State) ->
     game_session:process_request(GamePid, Message), 
     Module = State#context.module,
     catch Module:event({client,Message}),
-    {reply,<<"DATA">>,Req,State};
+    Actions = get(actions),
+    wf_context:clear_actions(),
+    Render = wf:render(Actions),
+    GenActions = get(actions),
+    RenderGenActions = wf:render(GenActions),
+    wf_context:clear_actions(),
+    {reply,wf:json([{eval,iolist_to_binary([Render,RenderGenActions])},
+                    {data,binary_to_list(term_to_binary(Message))}]),Req,State};
 
 info({send_message,Message}, Req, State) ->
     Module = State#context.module,
@@ -86,8 +95,8 @@ info({send_message,Message}, Req, State) ->
     GenActions = get(actions),
     RenderGenActions = wf:render(GenActions),
     wf_context:clear_actions(),
-    {reply,[<<"EVAL">>,Render,RenderGenActions],Req,State};
-%    {reply,[<<"DATA">>,io_lib:format("~p",[Message])],Req,State};
+    {reply,wf:json([{eval,iolist_to_binary([Render,RenderGenActions])},
+                    {data,binary_to_list(term_to_binary(Message))}]),Req,State};
 
 info(Pro, Req, State) ->
     Render = 
@@ -131,7 +140,8 @@ info(Pro, Req, State) ->
     wf_context:clear_actions(),
     RenderGenActions = wf:render(GenActions),
     wf_context:clear_actions(),
-    {reply, [<<"EVAL">>,Render,RenderGenActions], Req, State}.
+    JS = iolist_to_binary([Render,RenderGenActions]),
+    {reply, wf:json([{eval,JS}]), Req, State}.
 
 terminate(_Req, _State=#context{module=Module}) ->
     % wf:info("Bullet Terminated~n"),