socket.io的emit使用清单

function onConnect(socket){

  // 给本次连接的客户端发消息
  socket.emit('hello', 'can you hear me?', 1, 2, 'abc');

  // 给除了本次连接的其他所有连接者发消息
  socket.broadcast.emit('broadcast', 'hello friends!');

  // 给除了本次连接者之外的所有game房间的人发消息
  socket.to('game').emit('nice game', "let's play a game");

  //给除了本次连接者之外的所有game1、game2房间的人发消息
  socket.to('game1').to('game2').emit('nice game', "let's play a game (too)");

  // 给所有game中的人发消息
  io.in('game').emit('big-announcement', 'the game will start soon');

  // 给myNamespace命名空间的所有人发消息
  io.of('myNamespace').emit('bigger-announcement', 'the tournament will start soon');

  //给特定的socketid发消息(私信)
  socket.to(<socketid>).emit('hey', 'I just met you');

  // 发送需要回执的消息
  socket.emit('question', 'do you think so?', function (answer) {});

  // 发送无需压缩的消息
  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');

};

详情 https://blog.csdn.net/nathanhuang1220/article/details/41348213?utm_source=blogxgwz1

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这是一个远离手机的好地方,我一直在潜意识里提醒自己,我是一个网络原住民,然而事实呈现的我是一个依然会排斥网络的网络...
    丁口灿阅读 820评论 0 1
  • 毕业接近一年,如今的title是智能硬件产品经理,主攻智能终端OS。 从15岁开始,我就无比确信自己会踏入这一行,...
    XXCharles阅读 464评论 0 1
  • 森林大王狮子突发奇想要把天上的星星分给小动物们。并且每个小动物只许看属于自己的那颗星星,不许再看别人的。最不起眼的...
    3391阅读 622评论 0 1
  • 世界的本质是什么? 有人说了,世界的本质是意识,意识决定形态。 还有人说,世界的本质是物质,世界是由物质组成。 哪...
    跌代阅读 205评论 0 0