Send response to all clients except sender

前端 未结 10 1534
囚心锁ツ
囚心锁ツ 2020-11-22 14:26

To send something to all clients, you use:

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

To receive from clients, you use:

socket.on         


        
相关标签:
10条回答
  • 2020-11-22 15:00

    Here is my list (updated for 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) => {});
    
    0 讨论(0)
  • 2020-11-22 15:01

    Other cases

    io.of('/chat').on('connection', function (socket) {
        //sending to all clients in 'room' and you
        io.of('/chat').in('room').emit('message', "data");
    };
    
    0 讨论(0)
  • 2020-11-22 15:05

    Emit cheatsheet

    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');
    
    };
    
    0 讨论(0)
  • 2020-11-22 15:08

    Here is a more complete answer about what has changed from 0.9.x to 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
    

    I wanted to edit the post of @soyuka but my edit was rejected by peer-review.

    0 讨论(0)
  • 2020-11-22 15:09

    For namespaces within rooms looping the list of clients in a room (similar to Nav's answer) is one of only two approaches I've found that will work. The other is to use exclude. E.G.

    socket.on('message',function(data) {
        io.of( 'namespace' ).in( data.roomID ).except( socket.id ).emit('message',data);
    }
    
    0 讨论(0)
  • 2020-11-22 15:16

    I am using namespaces and rooms - I found

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

    to work where

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

    did not

    (should anyone else face that problem)

    0 讨论(0)
提交回复
热议问题