socket.io-restrict每个名称空间的最大连接数

我有一个build立在node.js,express和socket.io上的小webapp。 在那里,我使用了两个创build的命名空间:

lists = io.of('/lists'), views = io.of('/view'), 

我想要做的是限制/views命名空间中的连接数。 有没有办法使用socket.io做到这一点? 我看了一下这个文档,但是在那里找不到任何东西。

任何想法如何做到这一点?

谢谢你!

你可以做一个简单的计数器(如果需要的话 – 扩展课程):

 var lists = io.of('/lists'); lists.max_connections = 10; lists.current_connections = 0; lists.on('connection', function(socket){ if (this.current_connections >= this.max_connections) { socket.emit('disconnect', 'I\'m sorry, too many connections'); socket.disconnect(); } else { this.current_connections++; socket.on('disconnect', function () { this.current_connections--; }); /** something useful **/ } }); 

Upd:如果您希望客户端继续尝试连接到服务器,请使用:

 socket.conn.close(); 

代替:

 socket.disconnect();