在 cowboy websocket 处理程序中使用 gproc 注册两个本地进程时出现问题
Issue when registering two local process with gproc within cowboy websocket handler
我试图在 cowboy websocket 处理程序下使用 gproc 注册一堆具有唯一系列名称的进程。
在我的处理程序中,我创建了两个方法,第一个是处理注册:
websocket_handle({text, <<"Reg: ",Message/binary>>}, State) ->
io:format("Client ~p requesting to register ~n",[Message]),
MyPID=list_to_binary(pid_to_list(self())),
{[{_,Family}]}=jiffy:decode(Message),
io:format("Client ~p requesting to register ~n",[Family]),
Test = gproc:reg({p, l, Family}),
erlang:display(Test),
io:format("Registration OK, replying ..."),
Result = gproc:lookup_pids({p, l, Family}),
erlang:display(Result),
[PID] = Result,
io:format("PASS ~n"),
io:format("PID ~p FORMATTED ~n",[PID]),
Res= list_to_binary(pid_to_list(PID)),
\"inform\",\"From\" : \"Server\",\"Message\" : \"How you are doing !\"}">>),
{reply, {text,<<"{\"Type\" : \"fb_server\",\"Action\" : \"registration\",\"From\" : \"Server\",\"Message\" : \"",Res/binary,"\"}">>}, State};
第二个是处理小便调理:
websocket_handle({text, <<"Get: ",Message/binary>>}, State) ->
io:format("Client ~p requesting Pids ~n",[Message]),
{[{_,Family}]}=jiffy:decode(Message),
Result = gproc:lookup_pids({p, l, Family}),
erlang:display(Result),
if
Result == [] ->
{reply, {text,<<"{\"Type\" : \"fb_server\",\"Action\" : \"Get Pids\",\"From\" : \"Server\",\"Message\" : \"Empty list\"}">>}, State};
true ->
[PID] = Result,
io:format("PASS ~n"),
io:format("PID ~p FORMATTED ~n",[PID]),
Res= list_to_binary(pid_to_list(PID)),
\"fb_server\",\"Action\" : \"inform\",\"From\" : \"Server\",\"Message\" : \"How you are doing !\"}">>),
{reply, {text,<<"{\"Type\" : \"fb_server\",\"Action\" : \"Get Pids\",\"From\" : \"Server\",\"Message\" : \"",Res/binary,"\"}">>}, State}
end.
为了测试我的处理程序我创建了两个js文件,第一个是注册一个进程族,我开始注册请求如下:
writeToScreen("CONNECTED");
var msg = {family: "Js"};
websocket.send("Reg: "+JSON.stringify(msg) );
第二个测试文件是获取第一个文件已经注册的进程的pid:
function onOpen(evt)
{
//ON opening connection we will send a getPids request to get pids of processes registered under Family "Js"
writeToScreen("CONNECTED");
var msg = {family: "Js"};
//websocket.send("Reg: "+JSON.stringify(msg) );
getPids(msg);
//doSend("WebSocket rocks");
}
function getPids(msg)
{
writeToScreen("get Pids");
websocket.send("Get: "+JSON.stringify(msg) );
}
我的问题是第一个文件成功注册了进程,但第二个文件得到了一个空列表,基本上它应该得到一个列表,其中包含第一个文件已经创建的 pid ??
此致。
@Stefan Zobel,你是对的,在我的 onmessage 事件中我调用了 onclose() 事件。
我试图在 cowboy websocket 处理程序下使用 gproc 注册一堆具有唯一系列名称的进程。 在我的处理程序中,我创建了两个方法,第一个是处理注册:
websocket_handle({text, <<"Reg: ",Message/binary>>}, State) ->
io:format("Client ~p requesting to register ~n",[Message]),
MyPID=list_to_binary(pid_to_list(self())),
{[{_,Family}]}=jiffy:decode(Message),
io:format("Client ~p requesting to register ~n",[Family]),
Test = gproc:reg({p, l, Family}),
erlang:display(Test),
io:format("Registration OK, replying ..."),
Result = gproc:lookup_pids({p, l, Family}),
erlang:display(Result),
[PID] = Result,
io:format("PASS ~n"),
io:format("PID ~p FORMATTED ~n",[PID]),
Res= list_to_binary(pid_to_list(PID)),
\"inform\",\"From\" : \"Server\",\"Message\" : \"How you are doing !\"}">>),
{reply, {text,<<"{\"Type\" : \"fb_server\",\"Action\" : \"registration\",\"From\" : \"Server\",\"Message\" : \"",Res/binary,"\"}">>}, State};
第二个是处理小便调理:
websocket_handle({text, <<"Get: ",Message/binary>>}, State) ->
io:format("Client ~p requesting Pids ~n",[Message]),
{[{_,Family}]}=jiffy:decode(Message),
Result = gproc:lookup_pids({p, l, Family}),
erlang:display(Result),
if
Result == [] ->
{reply, {text,<<"{\"Type\" : \"fb_server\",\"Action\" : \"Get Pids\",\"From\" : \"Server\",\"Message\" : \"Empty list\"}">>}, State};
true ->
[PID] = Result,
io:format("PASS ~n"),
io:format("PID ~p FORMATTED ~n",[PID]),
Res= list_to_binary(pid_to_list(PID)),
\"fb_server\",\"Action\" : \"inform\",\"From\" : \"Server\",\"Message\" : \"How you are doing !\"}">>),
{reply, {text,<<"{\"Type\" : \"fb_server\",\"Action\" : \"Get Pids\",\"From\" : \"Server\",\"Message\" : \"",Res/binary,"\"}">>}, State}
end.
为了测试我的处理程序我创建了两个js文件,第一个是注册一个进程族,我开始注册请求如下:
writeToScreen("CONNECTED");
var msg = {family: "Js"};
websocket.send("Reg: "+JSON.stringify(msg) );
第二个测试文件是获取第一个文件已经注册的进程的pid:
function onOpen(evt)
{
//ON opening connection we will send a getPids request to get pids of processes registered under Family "Js"
writeToScreen("CONNECTED");
var msg = {family: "Js"};
//websocket.send("Reg: "+JSON.stringify(msg) );
getPids(msg);
//doSend("WebSocket rocks");
}
function getPids(msg)
{
writeToScreen("get Pids");
websocket.send("Get: "+JSON.stringify(msg) );
}
我的问题是第一个文件成功注册了进程,但第二个文件得到了一个空列表,基本上它应该得到一个列表,其中包含第一个文件已经创建的 pid ??
此致。
@Stefan Zobel,你是对的,在我的 onmessage 事件中我调用了 onclose() 事件。