how to keep alive a socket in node.js in appfog

2019-04-16 11:28发布

I have a long running socket which I need to keep alive even when the system is idle. For that I have tried everything from reduced long polling duration to a server and client ping pong messages but, nothing seems to work as the socket gets disconnected after some duration let's say 1 hour. But the socket connection needs to be alive till I manually close it.

How do I achieve this?

Also the latest version of node.js semms to support socket.setTimeOut(0) by which I can keep the socket open indefinitely but setTimeOut method seems to be non existent for socket in the version I am using as it shows method not defined error. The version of node I am using is 0.8.

For ping pong messages I am using this code http://stackoverflow.com/questions/9708604/nodejs-socket-io-connections-dropping-reconnecting

for reduced long poll duration I have used

http://stackoverflow.com/questions/13458943/node-js-tcp-socket-server-on-the-cloud-heroku-appfog

and for socket.settimeout on connection event

http://www.fingersdancing.net/2012/12/nodejs-connection-times-out-after-2.html

Any help on this will be much appreciated :)

1条回答
欢心
2楼-- · 2019-04-16 11:48

hmmmm... couldn't find the cause even though I believe it to be the evil work of the gfi web monitor the company is using but did find a solution...

The solution was to connect as soons as the disconnect event happened and again register the info on connect event (which is needed if only u r maintaing a separate list of info for the sockets ...say u r maintaing a list of online users)

socket.on("disconnect", function() {
        //https://github.com/LearnBoost/socket.io-client/issues/251

                    socket.socket.reconnect();

                });


socket.on("connect", function() {
                //do the registration code within this event
                });
查看更多
登录 后发表回答