'How to check if socket is still connected through socket id?

@socketio.on('connect',namespace='')
def connect():
    print("websocketConnect",request.sid)
    socketio.emit("on_connect",{"socketid":request.sid})

@socketio.on("disconnect",namespace='')
def disconnect():
    # socketio.emit("connect","hhhhh")
    print("websocket disconnected")
    print(request.sid)
    global queryLoginResultDict
    if queryLoginResultDict.__contains__(request.sid):
        queryLoginResultDict[request.sid].shutdown()
        del queryLoginResultDict[request.sid]
        print(request.sid,"DisConnected,shutdown")

I use the code above to find when a client is connected or disconnected in order to free some resources when a client is disconnected. it works fine on windows, the disconnect function works 30s after I shutdown the chrome, but it seems not work properly on linux, it shows connect, but seems disconnected doesn't work.

I'm wondering if there is a way that I can find whether a socket is still connected actively by using socket id ?



Solution 1:[1]

However I managed to find out why... when i use command python3 XXX.py ,all my prints show correctly like this

X.X.X.X - - [28/Feb/2022 00:52:21] "GET /socket.io/?EIO=3&transport=polling&t=Nyy68Ed&sid=4fa524fd36c64599a11c2fdd87dc790d HTTP/1.1" 200 - websocket disconnected 4fa524fd36c64599a11c2fdd87dc790d X.X.X.X - - [28/Feb/2022 00:52:30] "GET /socket.io/?EIO=3&transport=polling&t=Nyy68zz&sid=4fa524fd36c64599a11c2fdd87dc790d HTTP/1.1" 200 - w

but when i'm using command nohup python3 XXX.py >> XXX.log 2>&1 & it appears that all prints under socketio decoration are missing... only i see is logs like X.X.X.X - - [28/Feb/2022 00:52:21] "GET /socket.io/?EIO=3&transport=polling&t=Nyy68Ed&sid=4fa524fd36c64599a11c2fdd87dc790d HTTP/1.1" 200 -

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1