要向所有客户端发送内容,您可以使用:

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

要接收来自客户的信息,您可以使用:

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

如何将这两者结合起来,以便在服务器上从客户端接收消息时,将该消息发送给除发送消息的用户之外的所有用户?

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

我是否必须通过发送带有消息的client-id,然后在客户端检查来破解它,或者有更简单的方法吗?


当前回答

使用这个编码

io.sockets.on('connection', function (socket) {

    socket.on('mousemove', function (data) {

        socket.broadcast.emit('moving', data);
    });

这个socket.broadcast.emit()将发送函数中的所有内容,除了发送到发送的服务器

其他回答

对于房间中的命名空间,循环房间中的客户端列表(类似于Nav的回答)是我发现的两种可行的方法之一。另一种是使用排除。如

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

V4.x中的服务器端所有有效发射事件列表

io.on("connection", (socket) => {

// basic emit
socket.emit(/* ... */);

// to all clients in the current namespace except the sender
socket.broadcast.emit(/* ... */);

// to all clients in room1 except the sender
socket.to("room1").emit(/* ... */);

// to all clients in room1 and/or room2 except the sender
socket.to(["room1", "room2"]).emit(/* ... */);

// to all clients in room1
io.in("room1").emit(/* ... */);

// to all clients in room1 and/or room2 except those in room3
io.to(["room1", "room2"]).except("room3").emit(/* ... */);

// to all clients in namespace "myNamespace"
io.of("myNamespace").emit(/* ... */);

// to all clients in room1 in namespace "myNamespace"
io.of("myNamespace").to("room1").emit(/* ... */);

// to individual socketid (private message)
io.to(socketId).emit(/* ... */);

// to all clients on this node (when using multiple nodes)
io.local.emit(/* ... */);

// to all connected clients
io.emit(/* ... */);

// WARNING: `socket.to(socket.id).emit()` will NOT work, as it will send to everyone in the room
// named `socket.id` but the sender. Please use the classic `socket.emit()` instead.

// with acknowledgement
socket.emit("question", (answer) => {
    // ...
});

// without compression
socket.compress(false).emit(/* ... */);

// a message that might be dropped if the low-level transport is not writable
socket.volatile.emit(/* ... */);

});

客户端

// basic emit
    socket.emit(/* ... */);

// with acknowledgement
    socket.emit("question", (answer) => {
        // ...
    });

// without compression
    socket.compress(false).emit(/* ... */);

// a message that might be dropped if the low-level transport is not writable
    socket.volatile.emit(/* ... */);

存档链接到source => http://archive.today/2021.06.22-023900/https://socket.io/docs/v4/emit-cheatsheet/index.html

这里是一个更完整的答案,关于从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的帖子,但我的编辑被同行评审拒绝了。

发出备忘单

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');

};

我正在使用名称空间和房间-我发现

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

在哪里工作

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

没有

(如果其他人也面临同样的问题)