Envoyer une réponse à tous les clients sauf l'expéditeur

Pour envoyer quelque chose à tous les clients, vous utilisez:

io.sockets.emit('response', data); 

Pour recevoir des clients, vous utilisez:

 socket.on('cursor', function(data) { ... }); 

Comment puis-je combiner les deux afin que, lors de la réception d'un message sur le serveur à partir d'un client, j'envoie ce message à tous les utilisateurs, sauf celui qui envoie le message?

 socket.on('cursor', function(data) { io.sockets.emit('response', data); }); 

Dois-je le pirater en envoyant l'identifiant client avec le message, puis en vérifiant sur le côté client ou y a-t-il un moyen plus facile?

Voici ma liste (mise à jour pour 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'); 

À partir de la réponse @LearnRPG mais avec 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'); 

Pour répondre aux commentaires de @Crashalot, socketid provient de:

 var io = require('socket.io')(server); io.on('connection', function(socket) { console.log(socket.id); }) 

Voici une réponse plus complète sur ce qui a changé de 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 

Je voulais modifier le post de @soyuka, mais mon édition a été rejetée par un examen par les pairs.

Broadcast.emit envoie le msg à tous les autres clients (sauf pour l'expéditeur)

 socket.on('cursor', function(data) { socket.broadcast.emit('msg', data); }); 

Pour les espaces de noms dans les salles, la liste des clients dans une pièce (similaire à la réponse de Nav) est l'une des deux approches que j'ai trouvées qui fonctionneront. L'autre est d'utiliser exclure. PAR EXEMPLE

 socket.on('message',function(data) { io.of( 'namespace' ).in( data.roomID ).except( socket.id ).emit('message',data); } 

Autres cas

 io.of('/chat').on('connection', function (socket) { //sending to all clients in 'room' and you io.of('/chat').in('room').emit('message', "data"); }; 

Mise à jour de la liste pour plus de documentation.

 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. 

J'espère que cela t'aides.

Utiliser ce codage

 io.sockets.on('connection', function (socket) { socket.on('mousemove', function (data) { socket.broadcast.emit('moving', data); }); 

Ce socket.broadcast.emit () émettra tout dans la fonction sauf pour le serveur qui émet

J'utilise des espaces de noms et des chambres – j'ai trouvé

 socket.broadcast.to('room1').emit('event', 'hi'); 

Travailler là où

 namespace.broadcast.to('room1').emit('event', 'hi'); 

N'a pas

(Si quelqu'un d'autre devait faire face à ce problème)