要向所有客户端发送内容,您可以使用:
io.sockets.emit('response', data);
要接收来自客户的信息,您可以使用:
socket.on('cursor', function(data) {
...
});
如何将这两者结合起来,以便在服务器上从客户端接收消息时,将该消息发送给除发送消息的用户之外的所有用户?
socket.on('cursor', function(data) {
io.sockets.emit('response', data);
});
我是否必须通过发送带有消息的client-id,然后在客户端检查来破解它,或者有更简单的方法吗?
更新列表以获取进一步的文档。
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.
希望这能有所帮助。
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