如何知道何时订阅/取消订阅socket.io

刚刚search了所有的networking,以了解如何在通道订阅或取消订阅时跟踪node.js服务器。 我所能做的是知道的是连接和断开绑定,但不知道如何与渠道做到这一点。

io.sockets.on('connection', function (socket) { console.log("["+socket.id+"] Connected"); // handler to know when a socket subscribed a channel (*) ? // handler to know when a socket unsubscribed a channel (*) ? socket.on('disconnect', function () { console.log("["+socket.id+"] Disconnect"); }); }); 

可能吗?

您正在查找“socket.of('channelName')”…请参阅Socket.io文档

服务器:

 var io = require('socket.io').listen(80); var chat = io .of('/chat') .on('connection', function (socket) { socket.emit('a message', { that: 'only' , '/chat': 'will get' }); chat.emit('a message', { everyone: 'in' , '/chat': 'will get' }); }); var news = io .of('/news') .on('connection', function (socket) { socket.emit('item', { news: 'item' }); }); 

客户:

 <script> var chat = io.connect('http://localhost/chat') , news = io.connect('http://localhost/news'); chat.on('connect', function () { chat.emit('hi!'); }); news.on('news', function () { news.emit('woot'); }); </script>