问题
I'm working on a threejs animation that is recorded with ccapture
it depends on each client having a single connection to the node backend, which receives a 'render-frame' socket event.
This works for a while, but after a few minutes, the client, without error-ing out, closes the connection silently, and then creates a new connection, thus losing the first initial socket connection. I keep the window open the entire time and in focus.
my socket debug logs show the following. It always works for several frames, but always drops after a certain but not consistent amount of time:
socket.io:socket emitting event ["render-frame",{"frame":168}] +0ms
render frame
{ frame: 168 }
socket.io:client client close with reason transport close +6ms
socket.io:socket closing socket - reason transport close +0ms
socket.io:client ignoring remove for fbD1a4Wx0jBzJ7hqAAAA +1ms
SOCKET DISCONNECTED!
I simplified my render-frame listener on my node backend for debugging, and for now it simply looks like this:
io.on("connection", function (socket) {
socket.on("disconnect", function (socket) {
console.log("SOCKET DISCONNECTED!");
});
socket.on("render-frame", function (data) {
console.log(data);
});
});
This is happening in all browsers, though I only really need it to work in chrome. Using socket.io 1.3.7
Any help as to the cause of the 'transport close' error would be greatly appreciated.
回答1:
I think you should look at this question : NodeJS + Socket.io connections dropping/reconnecting?
Your problem is around socket timeouts. If there's no activity on a certain socket, socket.io will close it automatically.
An easy (and hackish) fix is to send a heartbeat to the connected client to create activity and stop the socket from timing out.
Server:
function sendHeartbeat(){
setTimeout(sendHeartbeat, 8000);
io.sockets.emit('ping', { beat : 1 });
}
io.sockets.on('connection', function (socket) {
socket.on('pong', function(data){
console.log("Pong received from client");
});
}
setTimeout(sendHeartbeat, 8000);
Client:
socket.on('ping', function(data){
socket.emit('pong', {beat: 1});
});
回答2:
It appears that HackTimer.js and ccapture.js both replace window.setTimeout with a custom function. HackTimer.js seems to work okay if it executes before any other JavaScript. For ccapture.js you may want to try to make sure it is run as the first script. So, SocketIO first uses your browser's native setTimeout that then gets blown away by the custom setTimeout which breaks any of the currently running timers.
来源:https://stackoverflow.com/questions/33136892/socketio-client-silently-loses-connection-and-creates-new-socket-transport-clos