美文网首页
socket.io Emit cheatsheet

socket.io Emit cheatsheet

作者: ZZES_ZCDC | 来源:发表于2018-05-01 09:33 被阅读39次

1.发送给客户端

  socket.emit('hello', 'can you hear me?', 1, 2, 'abc');

2.给所有客户端发送,除了发送的人

  socket.broadcast.emit('broadcast', 'hello friends!');

3.给「game」房间等所有用户发送,除了发送的人

  socket.to('game').emit('nice game', "let's play a game");

4.给「game1」和「game2」的用户发送,除了发送的人

  socket.to('game1').to('game2').emit('nice game', "let's play a game (too)");

5.给「game」房间所有的客户端发送,包括发送的人

  io.in('game').emit('big-announcement', 'the game will start soon');

6.给「myNamespace」命名空间下的所有客户端发送,包括发送的人

  io.of('myNamespace').emit('bigger-announcement', 'the tournament will start soon');

7.单独给指定socketid的客户端发送(私人信息)

  socket.to(<socketid>).emit('hey', 'I just met you');

8.发送确认信息

  socket.emit('question', 'do you think so?', function (answer) {});

9.不压缩发送

  socket.compress(false).emit('uncompressed', "that's rough");

如果客户端没有准备接收该信息,则发送等信息会被销毁

  socket.volatile.emit('maybe', 'do you really need it?');

给当前节点所有客户端发送(当使用多节点的时候)

  io.local.emit('hi', 'my lovely babies');

相关文章

网友评论

      本文标题:socket.io Emit cheatsheet

      本文链接:https://www.haomeiwen.com/subject/dygqlftx.html