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);
})


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM