Socket.io各个发送消息的含义【发送对象范围】

Socket.io各个发送消息的含义

 
// send to current request socket client
socket.emit('message', "this is a test");

// sending to all clients, include sender
io.sockets.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.sockets.in('game').emit('message', 'cool game');

// sending to individual socketid
io.sockets.socket(socketid).emit('message', 'for your eyes only');

// 进入一个房间
socket.join('room'); 
// 离开一个房间
socket.leave('room');

订阅/退订事件

//前端触发订阅/退订事件
socket.emit('subscribe',{"room" : "room_name"};
socket.emit('unsubscribe',{"room" : "room_name"};

//后台处理订阅/退订事件
socket.on('subscribe', function(data) { 
    socket.join(data.room);
})
socket.on('unsubscribe', function(data) { 
    socket.leave(data.room);
})
 
分类:  Socket
标签:  Socket.io

转载于:https://www.cnblogs.com/duanweishi/p/9281009.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值