N2O 协议在使用 gproc:send 时发送空数据
N2O protocol send an empty data when using gproc:send
我创建了一个 N2o 协议,通过不同的应用程序系列路由消息,(在此之前我使用牛仔 websocket 处理程序来完成这项工作)
例如,我的协议中有这段代码可以向已注册的进程发送消息
> 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};
在我的 HTML 客户端中,首先我根据
协议注册
var msg = {family: "JS"};
websocket.send("REG:"+JSON.stringify(msg) );
并且我验证了我的 html 进程已正确注册
然后,从我的同一个 HTML 客户端,我调用了传输协议方法,如:
msg = {Type: "TRANSMIT:", Action: "Chat", From: "JS", To: "JS",
Message:"Hello I am Js"};
websocket.send("TRANSMIT: "+JSON.stringify(msg) );
基本上在这个阶段我应该在我的 HTML 客户端收到一条包含 "Hello I am Js" 的消息,但我总是收到一个空数据字符串,如:
MessageEvent {isTrusted: true, data: "", origin:
"ws://192.168.1.20:8000", lastEventId: "", source: null…}
信息:
收到回复短信如下:
注册过程中:
MessageEvent {isTrusted: true, data: "process registered
successfully", origin: "ws://192.168.1.20:8000", lastEventId: "",
source: null…}
提交消息时:
MessageEvent {isTrusted: true, data: "message transmitted
successfully", origin: "ws://192.168.1.20:8000", lastEventId: "",
source: null…}
此外,当我使用我的 cowboy ws 处理程序时,我没有遇到此类问题。
The heart protocol defined client originated messages N2O, PING and server originated messages PONG, IO and NOP. IO message contains EVAL that contains UTF-8 JavaScript string and DATA reply contains any binary string, including BERT encoded data. "PING" and "N2O," are defined as text 4-bytes messages and second could be followed by any text string. NOP is 0-byte acknowledging packet.
来自 N2O 文档:http://synrc.com/apps/n2o/doc/web/protocols.htm#sec11
如果您想违反 N2O 协议,您应该创建自己的原始自定义协议模块:
-module(protocol).
-include_lib("n2o/include/wf.hrl").
-compile(export_all).
event(Event) -> ok.
finish(State,Ctx) -> {ok,State,Ctx}.
init(State,Ctx) -> wf:reg(topic), {ok,State,Ctx#cx{module=protocol}}.
info({text,Text}=Message, Req, State) -> {reply, Text, Req, State};
info(Message, Req, State) -> {unknown,Message, Req, State}.
并确保在 sys.config 中只为所有处理程序启用 protocol
:
{n2o, [{protocols,[protocol]},
{query,protocol},
{session,protocol},
{route,protocol}]},
用法:
wscat -c ws://127.0.0.1:9000/
connected (press CTRL+C to quit)
> echo
< echo
我创建了一个 N2o 协议,通过不同的应用程序系列路由消息,(在此之前我使用牛仔 websocket 处理程序来完成这项工作)
例如,我的协议中有这段代码可以向已注册的进程发送消息
> 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};
在我的 HTML 客户端中,首先我根据
协议注册var msg = {family: "JS"}; websocket.send("REG:"+JSON.stringify(msg) );
并且我验证了我的 html 进程已正确注册
然后,从我的同一个 HTML 客户端,我调用了传输协议方法,如:
msg = {Type: "TRANSMIT:", Action: "Chat", From: "JS", To: "JS", Message:"Hello I am Js"}; websocket.send("TRANSMIT: "+JSON.stringify(msg) );
基本上在这个阶段我应该在我的 HTML 客户端收到一条包含 "Hello I am Js" 的消息,但我总是收到一个空数据字符串,如:
MessageEvent {isTrusted: true, data: "", origin: "ws://192.168.1.20:8000", lastEventId: "", source: null…}
信息:
收到回复短信如下:
注册过程中:
MessageEvent {isTrusted: true, data: "process registered successfully", origin: "ws://192.168.1.20:8000", lastEventId: "", source: null…}
提交消息时:
MessageEvent {isTrusted: true, data: "message transmitted successfully", origin: "ws://192.168.1.20:8000", lastEventId: "", source: null…}
此外,当我使用我的 cowboy ws 处理程序时,我没有遇到此类问题。
The heart protocol defined client originated messages N2O, PING and server originated messages PONG, IO and NOP. IO message contains EVAL that contains UTF-8 JavaScript string and DATA reply contains any binary string, including BERT encoded data. "PING" and "N2O," are defined as text 4-bytes messages and second could be followed by any text string. NOP is 0-byte acknowledging packet.
来自 N2O 文档:http://synrc.com/apps/n2o/doc/web/protocols.htm#sec11
如果您想违反 N2O 协议,您应该创建自己的原始自定义协议模块:
-module(protocol).
-include_lib("n2o/include/wf.hrl").
-compile(export_all).
event(Event) -> ok.
finish(State,Ctx) -> {ok,State,Ctx}.
init(State,Ctx) -> wf:reg(topic), {ok,State,Ctx#cx{module=protocol}}.
info({text,Text}=Message, Req, State) -> {reply, Text, Req, State};
info(Message, Req, State) -> {unknown,Message, Req, State}.
并确保在 sys.config 中只为所有处理程序启用 protocol
:
{n2o, [{protocols,[protocol]},
{query,protocol},
{session,protocol},
{route,protocol}]},
用法:
wscat -c ws://127.0.0.1:9000/
connected (press CTRL+C to quit)
> echo
< echo