1

I've written a simple node.js/socket.io/express/redis server. I'm trying to get the clients to reinitialize when I kill and restart the server.

My client-side code looks like this:

var socket = io.connect('http://' + location.hostname); 
socket.on('connect', function (data) {socket.emit('join room', 'lobby' );});

My server-side code looks like this:

var SessionSockets = require('session.socket.io')
  , sessionSockets = new SessionSockets(io, sessionStore, cookieParser);

sessionSockets.on('connection', function (err, socket, session) {
    clientConnect(err, socket, session)
});

function clientConnect(err, socket, session){
  socket.on('join room', function (room) {
    if (room == 'lobby')
      lobby.connect(err,socket,session);
  });
}

EDIT: For some reason, when I kill the server (ctrl+c), and restart it, the connection handshake gets logged, and the server always emits a 'connect' but when the client follows suit and emits a 'join room' upon receiving the connect the server doesn't receive it (I'm doing a console.log on receiving a join room.)

However, if I put a client-side alert before emitting the 'join room' (forcing a delay essentially) then the code always works. Is it possible that the server hasn't fully initialized when the client sends the 'join room' event?

4

1 回答 1

1

这里有很多可能出错的地方,但首先你可以尝试收听有关重新连接的客户端事件..

客户端代码,重新连接时监听

socket.on('reconnecting',function(){ 
 socket.emit('join room','lobby');
});

(一个理论从来没有真正遇到过这个)

然后当应用程序停止时可能会出现异常行为,并且您正在使用 Redis 存储会话,您重新启动应用程序,并且会话仍然可用.. 解决此类问题

process.on("SIGINT",function(){
 // clean up
});

或者当应用程序启动时确保它清理会话存储,然后继续..

于 2013-09-29T19:24:25.443 回答