Issue when registering two local process with gproc within cowboy websocket handler

北战南征 提交于 2019-12-25 08:24:47

问题


I tried to register a bunch of processes with a unique family name with gproc under a cowboy websocket handler. In my handler I created two method, the first one is to handle registration:

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

The second one is to handle pis recuperation:

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.

To test my handler I created two js files, the first one is to register a process family, I start the registration request as follows:

 writeToScreen("CONNECTED");
var msg = {family: "Js"};  
websocket.send("Reg: "+JSON.stringify(msg) );

The second test file is to get the pid of process already registered by the first file:

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

My problem is that the first file register the process successfully but the second one get en empty list, basically it should get a list with the pid already created by the first file ??

Best regards .


回答1:


@Stefan Zobel, you are right,In my onmessage event I have a call to onclose() event.



来源:https://stackoverflow.com/questions/41955313/issue-when-registering-two-local-process-with-gproc-within-cowboy-websocket-hand

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!