2017-01-31 14 views
0

카우보이 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}; 

두 번째는 PIS 회복을 처리하는 것입니다

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와 목록을 얻어야한다, 첫 번째 파일이 성공적으로 과정을 등록하지만, 두 번째는 빈리스트 ko를 얻을 수 있다는 것입니다?

감사합니다.

+0

두 번째 클라이언트를 연결할 때 첫 번째 websocket이 아직 열려 있습니까? 첫 번째 클라이언트가 소켓을 닫으면 해당 websocket을 처리하는 erlang 프로세스가 종료되고 pid가 gproc 레지스트리에서 자동으로 제거됩니다. – johlo

답변

0

@Stefan Zobel, 네가 맞아. 내 onmessage 이벤트에서 onclose() 이벤트가 필요하다.