Socket.io를 사용하여 모든 클라이언트를 업데이트 하시겠습니까?
모든 클라이언트가 socket.io를 사용하여 업데이트하도록 강제 할 수 있습니까? 다음을 시도했지만 새 클라이언트가 연결될 때 다른 클라이언트를 업데이트하지 않는 것 같습니다.
서버 측 JavaScript :
현재 연결된 사용자 수를 포함하는 모든 클라이언트에 메시지를 보내려고합니다. 사용자 수를 올바르게 보냅니다 .... 그러나 클라이언트 자체는 페이지를 새로 고칠 때까지 업데이트되지 않는 것 같습니다. 실시간으로 이루어지기를 바랍니다.
var clients = 0;
io.sockets.on('connection', function (socket) {
++clients;
socket.emit('users_count', clients);
socket.on('disconnect', function () {
--clients;
});
});
클라이언트 측 JavaScript :
var socket = io.connect('http://localhost');
socket.on('connect', function(){
socket.on('users_count', function(data){
$('#client_count').text(data);
console.log("Connection");
});
});
실제로 다른 클라이언트에 업데이트를 보내는 것이 아니라 방금 연결된 클라이언트에 전송하는 것뿐입니다 (이것이 처음로드 할 때 업데이트를 보는 이유입니다).
// socket is the *current* socket of the client that just connected
socket.emit('users_count', clients);
대신 모든 소켓 에 방출하고 싶습니다.
io.sockets.emit('users_count', clients);
또는이를 시작하는 소켓을 제외한 모든 사람에게 메시지를 보내는 broadcast 함수를 사용할 수 있습니다.
socket.broadcast.emit('users_count', clients);
socket.broadcast.emit () 을 사용 하면 현재 "연결"에만 브로드 캐스트되지만 io.sockets.emit 는 모든 클라이언트에 브로드 캐스트된다는 것을 알았 습니다. 여기서 서버는 정확히 2 개의 소켓 네임 스페이스 인 "2 개의 연결"을 수신 합니다.
io.of('/namespace').on('connection', function(){
socket.broadcast.emit("hello");
});
io.of('/other namespace').on('connection',function(){/*...*/});
한 네임 스페이스에서 io.sockets.emit () 을 사용하려고 시도 했지만 다른 네임 스페이스의 클라이언트에서 수신했습니다. 그러나 socket.broadcast.emit () 은 현재 소켓 네임 스페이스를 브로드 캐스트합니다.
socket.io 버전 0.9부터 "emit"이 더 이상 작동하지 않았고 "send"를 사용했습니다.
내가하는 일은 다음과 같습니다.
서버 측 :
var num_of_clients = io.sockets.clients().length;
io.sockets.send(num_of_clients);
고객 입장에서:
ws = io.connect...
ws.on('message', function(data)
{
var sampleAttributes = fullData.split(',');
if (sampleAttributes[0]=="NumberOfClients")
{
console.log("number of connected clients = "+sampleAttributes[1]);
}
});
You can follow this example for implementing your scenario.
You can let all of clients to join a common room for sending some updates.
Every socket can join room like this:
currentSocket.join("client-presence") //can be any name for room
Then you can have clients key in you sockets which contains multiple client's data(id and status) and if one client's status changes you can receive change event on socket like this:
socket.on('STATUS_CHANGE',emitClientsPresence(io,namespace,currentSocket); //event name should be same on client & server side for catching and emiting
and now you want all other clients to get updated, so you can do something like this:
emitClientsPresence => (io,namespace,currentSocket) {
io.of(namespace)
.to(client-presence)
.emit('STATUS_CHANGE', { id: "client 1", status: "changed status" });
}
This will emit STATUS_CHANGE event to all sockets that have joined "client-presence" room and then you can catch same event on client side and update other client's status.
참고 URL : https://stackoverflow.com/questions/7352164/update-all-clients-using-socket-io
'Nice programing' 카테고리의 다른 글
프로그래밍 방식으로 홈 화면으로 이동 (0) | 2020.10.20 |
---|---|
CoffeeScript에서 (객체의 var 키)? (0) | 2020.10.20 |
Java에서 중괄호를 생략해도 괜찮습니까? (0) | 2020.10.20 |
sendAsynchronousRequest : queue : completionHandler를 사용하는 방법 : (0) | 2020.10.20 |
Symfony의 서비스에 저장소를 삽입하는 방법은 무엇입니까? (0) | 2020.10.20 |