向除发件人以外的所有客户发送回复
要发送给所有客户的东西,你使用:
io.sockets.emit('response', data);
为了接收客户,您使用:
socket.on('cursor', function(data) { ... });
如何将两者结合起来,以便在从客户端接收服务器上的消息时,将该消息发送给除发送消息的用户之外的所有用户?
socket.on('cursor', function(data) { io.sockets.emit('response', data); });
我必须通过发送客户端消息,然后检查客户端或有更简单的方法来破解它吗?
这是我的列表(更新为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');
从@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的post,但我的编辑被同行评审拒绝。
broadcast.emit将msg发送给所有其他客户端(发件人除外)
socket.on('cursor', function(data) { socket.broadcast.emit('msg', data); });
对于房间内的命名空间,循环房间中的客户列表(类似于Nav的答案)是我发现可行的两种方法之一。 另一种是使用排除。 例如
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"); };
更新列表进一步文件。
socket.emit('message', "this is a test"); //sending to sender-client only socket.broadcast.emit('message', "this is a test"); //sending to all clients except sender socket.broadcast.to('game').emit('message', 'nice game'); //sending to all clients in 'game' room(channel) except sender socket.to('game').emit('message', 'enjoy the game'); //sending to sender client, only if they are in 'game' room(channel) socket.broadcast.to(socketid).emit('message', 'for your eyes only'); //sending to individual socketid io.emit('message', "this is a test"); //sending to all clients, include sender io.in('game').emit('message', 'cool game'); //sending to all clients in 'game' room(channel), include sender io.of('myNamespace').emit('message', 'gg'); //sending to all clients in namespace 'myNamespace', include sender socket.emit(); //send to all connected clients socket.broadcast.emit(); //send to all connected clients except the one that sent the message socket.on(); //event listener, can be called on client to execute on server io.sockets.socket(); //for emiting to specific clients io.sockets.emit(); //send to all connected clients (same as socket.emit) io.sockets.on() ; //initial connection from a client.
希望这可以帮助。
使用这个编码
io.sockets.on('connection', function (socket) { socket.on('mousemove', function (data) { socket.broadcast.emit('moving', data); });
这个socket.broadcast.emit()将在函数中发射everthing,除了正在发射的服务器
我正在使用命名空间和房间 – 我发现
socket.broadcast.to('room1').emit('event', 'hi');
去哪里工作
namespace.broadcast.to('room1').emit('event', 'hi');
没有
(其他人应该面对这个问题)