启动时节点socket.io服务器崩溃 [英] Node socket.io server crash when starts
问题描述
我有一个socket.io服务器和一个客户端运行正常.每次服务器关闭时,客户端都会尝试每5秒重新连接一次.当服务器再次启动时,它们可以正常连接.
I have a socket.io server and a client runing correctly. Each time that the server is down, the client try to connect again each 5 seconds. When the server is up again they connect without problems.
但是问题出在我等待很长时间再重新启动服务器时,当服务器启动时,它崩溃,显示为:
But the problem comes when I wait long time before up the server again, when the server is up, it crashes showing :
info - socket.io started
debug - client authorized
info - handshake authorized DqN4t2YVP7NiqQi8zer9
debug - setting request GET /socket.io/1/websocket/DqN4t2YVP7NiqQi8zer9
debug - set heartbeat interval for client DqN4t2YVP7NiqQi8zer9
debug - client authorized for
debug - websocket writing 1::
buffer.js:287
pool = new SlowBuffer(Buffer.poolSize);
^
RangeError: Maximum call stack size exceeded
客户端重新连接(未连接时每5秒执行一次):
Client reconnection (Executed each 5 seconds while is not connected):
function socket_connect() {
if (!socket) {
socket = io.connect('http://192.168.1.25:8088', { 'reconnect':false, 'connect timeout': 5000 });
} else {
socket.socket.connect();
}
socket.on("connect", function () {
clearInterval(connect_interval);
connect_interval = 0;
socket.emit('player', { refresh_data:true });
});
}
在服务器端,仅对于套接字实例,它会崩溃:
On server side, only with the socket instance, it crashes:
var io = require('socket.io').listen(8088);
我认为问题是:
服务器启动时,它每5秒恢复一次客户端发出的所有连接(15小时断开* 60 m * 60 s/5秒重新连接),并且崩溃.
When the server goes up, it recive all the connections emited by the client each 5 seconds, (15 hours disconnected * 60 m * 60 s / 5 seconds reconnection) and it crashes.
我该怎么做才能关闭服务器尝试执行的连接?
What can i do to close the connections that the server try to do?
PS:如果我重新加载客户端,并且在服务器启动之后,它就可以工作
PS:If i reload the client, and after up the server, it works
推荐答案
我发现了问题...
每次调用函数socket_connect()时,都会创建一个"socket.on(" connect"...")函数.因此,当服务器启动时,将创建一个新的连接,但事件为"socket.on("connect"被触发多次...
Each time that the function socket_connect() is called, a "socket.on("connect" ..." function is created. So when the server turns up, a new connection is created, but the event "socket.on("connect" is fired multiple times...
解决方案是:
function socket_connect() {
if (!socket) {
socket = io.connect('http://192.168.1.25:8088', { 'reconnect':false, 'connect timeout': 5000 });
} else {
socket.socket.connect();
}
}
socket.on("connect", function () {
clearInterval(connect_interval);
connect_interval = 0;
socket.emit('player', { refresh_data:true });
});
这篇关于启动时节点socket.io服务器崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!