javascript Socket.io广播

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Socket.io广播相关的知识,希望对你有一定的参考价值。

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

以上是关于javascript Socket.io广播的主要内容,如果未能解决你的问题,请参考以下文章

socket.io - 如何在命名空间上广播消息?

Socket.io 广播到房间不工作

socket.io:为啥这个广播语句不起作用?

Socket.io广播到房间不工作

socket.io 广播功能 & Redis pub/sub 架构

使用 socket.io 广播网络摄像头?