马会东的博客

马会东的博客

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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
posted on 2018-07-08 19:27  马会东  阅读(364)  评论(0编辑  收藏  举报