在牛仔 websocket 处理程序中向 gproc 注册两个本地进程时出现问题



我试图在牛仔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};

第二个是处理 pi 回收:

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) );
}

我的问题是第一个文件成功注册了进程,但第二个文件得到了 en 空列表,基本上它应该得到一个列表,其中包含第一个文件已经创建的 pid?

此致敬意。

@Stefan Zobel,你是对的,在我的onmessage事件中,我调用了onclose()事件。

最新更新