synrc / n2o

⭕ N2O: Distributed WebSocket Application Server ISO 20922
https://ws.n2o.dev
Other
1.34k stars 168 forks source link

N2O protocol send an empty data when using gproc:send #293

Closed hamdijmii1992 closed 7 years ago

hamdijmii1992 commented 7 years ago

I created an N2o protocol that routes messages over different family of application, (before that I used a cowboy websocket handler to do this job)

So for example I have this code in my protocol to send a message to a registered process

info({text,<<"REG:",Msg/binary>> }, Req, State) ->
     {[{_,Family}]}=jiffy:decode(Msg),
    Test = gproc:reg({p, l, Family}, 1), 
    {reply, <<"process registered successfully">>, Req, State};
info({text,<<"TRANSMIT:",Msg/binary>> }=M, Req, State) ->
    {[{_,Type},{_,Action},{_,From},{_,To},{_,Message}]}=jiffy:decode(Msg),
     gproc:send({p, l, To}, {self(),<<"Hi I am a good message!">>}),
      {reply, <<"message transmitted successfully">>, Req, State};

In my HTML client , firstly I register under the protocol with

 var msg = {family: "JS"};
    websocket.send("REG:"+JSON.stringify(msg) );

And I verified that my html process was well registered

Then , From My same HTML client, I called the transmit protocol method like:

var msg = {Type: "TRANSMIT:", Action: "Chat", From: "JS", To: "JS", Message: "Hello I am Js"}; 
websocket.send("TRANSMIT: "+JSON.stringify(msg) );

Basically in this stage I should receive a message contain "Hello I am Js" in my HTML client, but Always I received an empty data string like:

MessageEvent {isTrusted: true, data: "", origin: "ws://192.168.1.20:8000", lastEventId: "", source: null…} INFO:

I well received the reply message as follows:

when registering process: MessageEvent {isTrusted: true, data: "process registered successfully", origin: "ws://192.168.1.20:8000", lastEventId: "", source: null…}

when submitting message: MessageEvent {isTrusted: true, data: "message transmitted successfully", origin: "ws://192.168.1.20:8000", lastEventId: "", source: null…}

Also When I used my cowboy ws handler I didn't experience such issue.

5HT commented 7 years ago

<<>> is not an issue but a part of N2O protocol

https://github.com/synrc/n2o/blob/master/src/endpoints/n2o_proto.erl#L40

You can just ignore <<>>.

5HT commented 7 years ago

Here is an example of CUSTOM TEXT protocol without <<>> messages.

https://github.com/spawnproc/tic/blob/master/src/protocol.erl