0

I want to send an event back to a client, in Socket.io, and immediately after that to disconnect the client. The problem is that calling socket.emit() will not emit anything... Do you know, or stepped into this kind of problem?

my code is like this: - on server:

userCredentialsCheck: function (socket, next, callback) {
    appLogSys.info('Check cookie!');
    .....
    var handshake = socket.request;
    .....
    parseCookie(handshake, null, function (err, data) {
        sessionSrv.get(handshake, function (err, session) {
            if (err) {
                appLogSys.error(err.message);
                next(new Error(err.message));
            }
            else
            {
                ......
                socket.emit('na', 'Error checking session! Good bye!');
                socket.disconnect();
            }
          })
    })
};

on client:

appSocket.on('na', function(d){
        console.log('Error: '+d);
        console.log('Now, redirect!');
        //REDIRECT!
        var delay = 1000; //Your delay in milliseconds
        var URL   = '/';
        setTimeout(function(){ window.location = URL; }, delay);
    });

Thank you!

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • does it emit if you dont' disconnect? – Kevin B Jun 19 '15 at 19:03
  • no, I tried without disconnect and still nothing... – Cosmin Panait Jun 19 '15 at 19:10
  • Then you haven't narrowed down the code enough. If the act of disconnecting has no effect on the actual problem, that shouldn't be part of the question. – Kevin B Jun 19 '15 at 19:11
  • At this point the problem is either: `err` is truethy, or `socket.emit` is failing to emit. next logical step would be to test the value of err, and then to test socket.emit on it's own without the authentication portion. – Kevin B Jun 19 '15 at 19:13
  • Kevin B, disconnecting works, it disconnects the socket, the problem is that it doesn't emit the event to the client... – Cosmin Panait Jun 19 '15 at 19:13
  • disconnecting it works, and removing the disconnect doesn't make it work, so leaving it removed and continuing to debug is the correct path. – Kevin B Jun 19 '15 at 19:16
  • Thank you, I will continue debugging...If i find something I will keep you posted. – Cosmin Panait Jun 19 '15 at 19:18
  • Wouldn't it be better to use AJAX long polling? It'll be faster to connect, and simpler to implement. – Kornel Jun 19 '15 at 20:00

0 Answers0