5

In my index.html (HTML/Javascript) I have:

$(document).ready(function(){
        namespace = '/test'; 

        var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);

        socket.on('connect', function() {
            socket.emit('join', {room: 'venue_1'}); 
        });       


        socket.on('my response', function(msg) {
            $('#log').append('<br>Received #' + ': ' + msg.data);
        });       
    });

On my Server I have:

@socketio.on('connect', namespace='/test')
def test_connect():
    if session.get('venue_id'):
        emit('my response', {'data': 'Connected'})      
        session.pop('venue_id', None)
    else:
        request.namespace.disconnect() 

@socketio.on('join', namespace='/test')
def join(message):
    join_room(message['room'])
    room = message['room']  
    emit('my response', {'data': 'Entered the room ' + message['room']})

After logging in, I set session['venue_id'] = True and move to index.html. The output I get is:

Received #: Connected
Received #: Entered the room venue_1

My question: After the initial run, I keep the index.html page open and then stop and start my project through supervisor. At this point why do I get the same output as above? I would have thought that after the initial connect, venue_id would have been removed from the session and hence request.namespace.disconnect() would be called?

Could someone please explain to me the sequence of events here?

Thanks

1 Answers1

2

The Socket.IO client has a reconnect logic built in. If the server goes away there is the expected disconnect, but right away the client starts to connect again, and obviously succeeds very quickly since the restart has a very short down time.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152