我正在编写一个TCP / IP队列,该队列应该将最大连接用户限制为3,并让其他任何人等到用户断开连接。这些是我使用的功能:
var maxClients = 3;
var currentClients = 0;
var _pending = [];
function process_pending()
{
if (_pending.length > 0) {
var client = _pending.shift();
currentClients++;
client(function()
{
currentClients--;
process.nextTick(process_pending);
});
}
}
function client_limit(client)
{
if (currentClients < maxClients) {
currentClients++;
client(function()
{
currentClients--;
process.nextTick(process_pending);
});
}
else
{
console.log('Overloaded, queuing clients...');
_pending.push(client);
}
}
每当连接三个客户端,然后第四个客户端尝试连接时,它会显示Overloaded消息,这没关系,但是如果现有客户端断开连接,则没有其他人可以连接。看起来像
client(function()
{
currentClients--;
process.nextTick(process_pending);
});
无法正常使用,我该如何解决?