最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS socket IO stop emitting randomly? - Stack Overflow

programmeradmin2浏览0评论

I'm running Node 0.6.16 and all the modules are up to date at least according to npm (win7 x64). I could notice that, even if I have no disconnection occurring, but for some reason, after a while, I couldn't tell, maybe 1 hour, the browser doesn't receive any data. It appears to be more frequent on FF than on Chrome.

socket.on('disconnect', function (){console.log('disconnected')});

This never fires for that particular event (I mean it could happen but then I get the log and socket.io auto reconnects - In this case, nothing happens, it just stops working and this is the most frequent event).

So I don't know where to look. NodeJS still logs an heartbeat but for some reason, the connection is not going through. I am suspecting that, on tab change of (any?) browser, after a while, as the window / tab doesn't have focus anymore, socket.io stops receiving data ? I could be wrong and it could be not related to the focus, but this is my primary lead. Anyone else got any ideas?

Edit: client side is pretty straight forward :

socket = io.connect(':8081', {secure:false, 'reconnect': true, 'reconnection delay': 500, 'max reconnection attempts': 10});      
            socket.on('connect', function(){console.log('connected')});
            socket.on('message', function(data){//do something);        

            socket.on('disconnect', function (){console.log('disconnected')});

Edit2 : After updating to latest version of socket.io (1.2.1) and node v0.10.35 I still have the same issue. Even more surprising, I added the following piece of code in the client:

function checkSocket(){
    if(socket){
        if(!socket.connected){
            console.log('socket disconnected');
            clearInterval(intv);
        }
    }       
}

intv = setInterval(checkSocket, 1000);

While the function runs every second, it never logs that the socket is disconnected, even if it does not receive anything anymore and still being sent heartbeats..

Edit3: Ok it was on my server side code, some socket were being destroyed. Also updated to v1.x and forced reconnection.

I'm running Node 0.6.16 and all the modules are up to date at least according to npm (win7 x64). I could notice that, even if I have no disconnection occurring, but for some reason, after a while, I couldn't tell, maybe 1 hour, the browser doesn't receive any data. It appears to be more frequent on FF than on Chrome.

socket.on('disconnect', function (){console.log('disconnected')});

This never fires for that particular event (I mean it could happen but then I get the log and socket.io auto reconnects - In this case, nothing happens, it just stops working and this is the most frequent event).

So I don't know where to look. NodeJS still logs an heartbeat but for some reason, the connection is not going through. I am suspecting that, on tab change of (any?) browser, after a while, as the window / tab doesn't have focus anymore, socket.io stops receiving data ? I could be wrong and it could be not related to the focus, but this is my primary lead. Anyone else got any ideas?

Edit: client side is pretty straight forward :

socket = io.connect('http://xxx.xxx.xxx.xxx:8081', {secure:false, 'reconnect': true, 'reconnection delay': 500, 'max reconnection attempts': 10});      
            socket.on('connect', function(){console.log('connected')});
            socket.on('message', function(data){//do something);        

            socket.on('disconnect', function (){console.log('disconnected')});

Edit2 : After updating to latest version of socket.io (1.2.1) and node v0.10.35 I still have the same issue. Even more surprising, I added the following piece of code in the client:

function checkSocket(){
    if(socket){
        if(!socket.connected){
            console.log('socket disconnected');
            clearInterval(intv);
        }
    }       
}

intv = setInterval(checkSocket, 1000);

While the function runs every second, it never logs that the socket is disconnected, even if it does not receive anything anymore and still being sent heartbeats..

Edit3: Ok it was on my server side code, some socket were being destroyed. Also updated to v1.x and forced reconnection.

Share Improve this question edited May 21, 2015 at 16:08 José Ricardo Pla 1,04310 silver badges16 bronze badges asked May 11, 2012 at 0:43 EricEric 10.7k14 gold badges73 silver badges111 bronze badges 14
  • currently im doing the same set (node.js and socket.io), it works perfectly for me on all browsers, may be you could show us some of your client side code – Dhiraj Commented May 12, 2012 at 15:22
  • 1 I think this must resolved by now. – Farid Nouri Neshat Commented Apr 15, 2014 at 18:33
  • I'm also having this problem. My project is a radio that needs the sockets to get the currently playing song/cover. Long polling worked before but failed for a large amount of requests. I ended up binding to window blur and focus with a setTimeout that emits a request to rejoin the room (which then resends the relevant data). Hoping to find a better solution one day! – youanden Commented Sep 26, 2014 at 0:21
  • Thanks for the ment. I still haven't figured it out... – Eric Commented Sep 26, 2014 at 1:07
  • Could you make a full example that we can experiment with? I suspect that this isn't really a problem and is as-documented. Socket.IO doesn't really get disconnected unless something catastrophic happens. Many transitory connection issues are hidden and swallowed by Socket.IO itself. Also, what transport(s) are you using? Finally, if you could record a packet capture, that would be very helpful. – Brad Commented Dec 29, 2014 at 1:14
 |  Show 9 more ments

1 Answer 1

Reset to default 0

So since it seems I have no more issue, I thought I would share my back-end code. My issue I believe was that I was overwriting the socket.

var clients = {};
var app = require('https').createServer(options),
io = require('socket.io').listen(app);  
app.listen(8081);

io.sockets.on('connection', function(__socket) {
    var id = __socket.id;
    var ip = __socket.request.connection.remoteAddress;
    clients[id] = {socket:__socket, ip:ip}; 
    clients[id].socket.on('disconnect', function() {
        delete clients[id];     
   });
});
发布评论

评论列表(0)

  1. 暂无评论