[javascript] 발신자를 제외한 모든 고객에게 응답 보내기
모든 고객에게 무언가를 보내려면 다음을 사용하십시오.
io.sockets.emit('response', data);
클라이언트로부터 수신하려면 다음을 사용하십시오.
socket.on('cursor', function(data) {
...
});
클라이언트에서 서버로 메시지를 수신 할 때 메시지를 보내는 사람을 제외한 모든 사용자에게 해당 메시지를 보내도록이 둘을 어떻게 결합 할 수 있습니까?
socket.on('cursor', function(data) {
io.sockets.emit('response', data);
});
메시지와 함께 클라이언트 ID를 보낸 다음 클라이언트 쪽을 확인하여 해킹해야합니까? 아니면 더 쉬운 방법이 있습니까?
답변
다음은 내 목록입니다 (1.0으로 업데이트 됨) .
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'cool game');
// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');
// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');
// sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'for your eyes only');
// list socketid
for (var socketid in io.sockets.sockets) {}
OR
Object.keys(io.sockets.sockets).forEach((socketid) => {});
답변
@LearnRPG 답변에서 1.0이지만 :
// send to current request socket client
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.sockets.emit('message', "this is a test"); //still works
//or
io.emit('message', 'this is a test');
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
// docs says "simply use to or in when broadcasting or emitting"
io.in('game').emit('message', 'cool game');
// sending to individual socketid, socketid is like a room
socket.broadcast.to(socketid).emit('message', 'for your eyes only');
@Crashalot 의견에 대한 답변 socketid
은 다음과 같습니다.
var io = require('socket.io')(server);
io.on('connection', function(socket) { console.log(socket.id); })
답변
다음은 0.9.x에서 1.x로 변경된 사항에 대한보다 완벽한 답변입니다.
// send to current request socket client
socket.emit('message', "this is a test");// Hasn't changed
// sending to all clients, include sender
io.sockets.emit('message', "this is a test"); // Old way, still compatible
io.emit('message', 'this is a test');// New way, works only in 1.x
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");// Hasn't changed
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');// Hasn't changed
// sending to all clients in 'game' room(channel), include sender
io.sockets.in('game').emit('message', 'cool game');// Old way, DOES NOT WORK ANYMORE
io.in('game').emit('message', 'cool game');// New way
io.to('game').emit('message', 'cool game');// New way, "in" or "to" are the exact same: "And then simply use to or in (they are the same) when broadcasting or emitting:" from http://socket.io/docs/rooms-and-namespaces/
// sending to individual socketid, socketid is like a room
io.sockets.socket(socketid).emit('message', 'for your eyes only');// Old way, DOES NOT WORK ANYMORE
socket.broadcast.to(socketid).emit('message', 'for your eyes only');// New way
@soyuka의 게시물을 편집하고 싶었지만 피어 리뷰에서 편집을 거부했습니다.
답변
broadcast.emit는 msg를 다른 모든 클라이언트에게 보냅니다 (발신인 제외)
socket.on('cursor', function(data) {
socket.broadcast.emit('msg', data);
});
답변
치트 시트 방출
io.on('connect', onConnect);
function onConnect(socket){
// sending to the client
socket.emit('hello', 'can you hear me?', 1, 2, 'abc');
// sending to all clients except sender
socket.broadcast.emit('broadcast', 'hello friends!');
// sending to all clients in 'game' room except sender
socket.to('game').emit('nice game', "let's play a game");
// sending to all clients in 'game1' and/or in 'game2' room, except sender
socket.to('game1').to('game2').emit('nice game', "let's play a game (too)");
// sending to all clients in 'game' room, including sender
io.in('game').emit('big-announcement', 'the game will start soon');
// sending to all clients in namespace 'myNamespace', including sender
io.of('myNamespace').emit('bigger-announcement', 'the tournament will start soon');
// sending to individual socketid (private message)
socket.to(<socketid>).emit('hey', 'I just met you');
// sending with acknowledgement
socket.emit('question', 'do you think so?', function (answer) {});
// sending without compression
socket.compress(false).emit('uncompressed', "that's rough");
// sending a message that might be dropped if the client is not ready to receive messages
socket.volatile.emit('maybe', 'do you really need it?');
// sending to all clients on this node (when using multiple nodes)
io.local.emit('hi', 'my lovely babies');
};
답변
룸 내 네임 스페이스의 경우 룸 내 클라이언트 목록 (Nav의 답변과 유사)은 내가 찾은 두 가지 접근법 중 하나입니다. 다른 하나는 제외를 사용하는 것입니다. EG
socket.on('message',function(data) {
io.of( 'namespace' ).in( data.roomID ).except( socket.id ).emit('message',data);
}
답변
다른 경우
io.of('/chat').on('connection', function (socket) {
//sending to all clients in 'room' and you
io.of('/chat').in('room').emit('message', "data");
};