我的 socket.io Nodejs 应用程序(在 Windows Azure 中)遇到问题。它工作正常,但过了一会儿我收到一个错误,服务器回复如下:
HTTP/1.1 503 事件 WebSocket 请求数已达到允许的最大并发 WebSocket 请求数
- 我使用的是 Windows Azure 的基本计划,根据这篇文章 https://azure.microsoft.com/en-us/blog/introduction-to-websockets-on-windows-azure-web-sites/我可以拥有 350 个并发连接。
- 我使用 Windows Azure 日志来计算事件套接字的数量,在 350 个套接字连接(和断开连接)的确切位置,服务器回复了错误。
服务器设置:
var client_id = 0;
var connectCounter = 0;
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
console.log('server init.');
}).listen(port);
io = io.listen(server);
io.sockets.on('connection', function(socket)
{
socket.client_id = client_id;
client_id++;
connectCounter++;
console.log('A socket with client_id ' + socket.client_id + ' connected!');
console.log('Sockets counter: ' + connectCounter);
socket.on('disconnect', function() {
console.log('A socket with client_id ' + socket.client_id + ' disconnected!');
connectCounter--;
console.log('Sockets counter: ' + connectCounter);
});
});
最后的示例日志:
A socket with client_id 349 connected!
Sockets counter: 1
A socket with client_id 350 connected!
Sockets counter: 2
A socket with client_id 349 disconnected!
Sockets counter: 1
A socket with client_id 350 disconnected!
Sockets counter: 0
由于我的套接字正在连接和断开连接,因此不应有 350 个并发连接。我做错了什么?
最佳答案
根据我的评论,该问题很可能存在于 engine.io
模块中的错误,该模块是 socket.io
的一个组件。表面上看,原因是 engine.io
在检测到 ping 超时时没有关闭 tcp 套接字。
在GitHub issue I found中,他们显然通过添加对位于路径的文件的 self.transport.close()
调用来修补该错误:
node_modules/socket.io/node_modules/engine.io/lib/socket.js
它应该位于您的 Node 应用程序目录中的某个位置。
我在 this GitHub pull 找到了这行代码。代码如下:
Socket.prototype.setPingTimeout = function () {
var self = this;
clearTimeout(self.pingTimeoutTimer);
self.pingTimeoutTimer = setTimeout(function () {
self.onClose('ping timeout');
self.transport.close(); // This is the added line
}, self.server.pingInterval + self.server.pingTimeout);
};
尝试一下,它可能会解决您的问题。
关于Node.js - Socket.io - 问题 "maximum concurrent connections",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33520032/