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/natha

 

======================================================

    1. // send to current request socket client
    2.  socket.emit( 'message', "this is a test");
    3.   // sending to all clients except sender
    4.  socket.broadcast.emit( 'message', "this is a test");
    5.   // sending to all clients in 'game' room(channel) except sender
    6.  socket.broadcast.to( 'game').emit('message', 'nice game');
    7.   // sending to all clients, include sender
    8.  io.sockets.emit( 'message', "this is a test");
    9.   // sending to all clients in 'game' room(channel), include sender
    10.  io.sockets. in('game').emit('message', 'cool game');
    11.   // sending to individual socketid
    12.  io.sockets.socket(socketid).emit( 'message', 'for your eyes only');




转载于:https://www.cnblogs.com/suneil/p/11287825.html

这段代码实现了一个并查集数据结构。并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。它支持两种操作: - 查找(Find):确定元素属于哪一个子集。它可以被用来确定两个元素是否属于同一子集。 - 合并(Union):将两个子集合并成同一个集合。 并查集可以用于解决很多实际问题,例如: - 判断无向图中是否有环 - 在图像处理中,判断连通区域 - 在游戏开发中,判断游戏中角色的关系 在这段代码中,类 UnionFindSet 的初始化函数 __init__ 接收两个参数:起始点 start 和结束点 n。接着定义了两个列表 pre 和 rank,用于存储每个节点的父节点和树的深度。其中,pre[i] 表示节点 i 的父节点,如果 pre[i] = i,则 i 为该集合的代表元素。 接下来的函数 init 用于初始化并查集,将每个节点的父节点设置为自身,深度为 1。 函数 find_pre 用于查找节点 x 的代表元素,同时实现了路径压缩的优化,即将查找路径上的所有节点都直接连接到代表元素上,减少查找时间。 函数 is_same 用于判断节点 x 和节点 y 是否在同一个集合中,即是否具有相同的代表元素。 函数 unite 用于合并两个集合,即将 x 所在的集合和 y 所在的集合合并为一个集合。首先查找 x 和 y 的代表元素,如果它们已经在同一个集合中,则直接返回 False。否则,将深度较小的集合连接到深度较大的集合上,并更新代表元素和深度。 最后,函数 is_one 用于判断整个并查集是否只有一个集合。它首先找到起始点的代表元素 temp,然后遍历起始点到结束点之间的所有节点,如果存在任意一个节点的代表元素不等于 temp,则说明存在多个集合,返回 False;否则,所有节点都在同一个集合中,返回 True。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值