2011-12-15 4 views
2

나는 socket.io와 함께이 시나리오를 가지고있다.Socket.io on socket disconnect

소켓은 방을 연결하고 방에 들어가며 그는 주인이다. 다른 소켓이 그의 방에 들어갑니다. 마스터가 연결을 끊으면이 방의 다른 소켓을 모두 차고 싶습니다.

나는이 생각 :

socket.on('disconnect', function(data){ 
    // if socket's id == room he is the master and kick other sockets from this 
    // room and join them to a room of their own identified by their ids.  
}); 

내가 너무 많은 로직없이 응용 프로그램을 실속 루프에 대해이 작업을 수행하고자합니다. io.sockets.leave(socket.room)과 같은 것이 가능합니까?

답변

4

나는 100 % 확신하지만, GitHub의에 Socket.IO's documentation을 읽은 후 나는이 일을해야한다고 생각 :이 소켓의 배열에 전화를 "떠나"하려고하기 때문에

io.sockets.in(socket.room).leave(socket.room); 
4

alessioalex의 대답은 오류를 반환합니다. 해결책은 다음과 같습니다.

io.sockets.clients(socket.room).forEach(function(listener) { 
    listener.leave(socket.room); 
}) 
+1

당신은 그의 대답을 편집 할 수있었습니다. – mak