在cowboy websocket处理程序中使用gproc注册两个本地进程时出现问题 [英] Issue when registering two local process with gproc within cowboy websocket handler

查看:222
本文介绍了在cowboy websocket处理程序中使用gproc注册两个本地进程时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用gproc在牛仔websocket处理程序下注册一系列具有独特姓名的进程。
在我的处理程序中,我创建了两个方法,第一个是处理注册:

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

第二个是处理pis recup eration:

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.

要测试我的处理程序,我创建了两个js文件,第一个是注册一个进程族,我开始注册请求如下:

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

第二个测试文件是获取进程的pid已经由第一个文件注册:

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

我的问题是第一个文件成功注册过程,但第二个文件获取en空列表,基本上应该得到一个已经由第一个文件创建的pid的列表?

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 ??

最好的问候。

推荐答案

@Stefan Zobel,你是对的,在我的onmessage事件中我打电话给onclose()事件。

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

这篇关于在cowboy websocket处理程序中使用gproc注册两个本地进程时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆