Eugene
Eugene

Reputation: 1125

Python socket.io client events

I am using this Python socket.io client library. This is part of the python code which is used to instantiate and connect to the socket io server:

import socketio

sio = socketio.Client()
logger.info('Created socketio client')
sio.connect('https://server.com/socket.io/')

@sio.event
def connect():
    logger.info('connected to server')

@sio.event
def disconnect():
    logger.info('disconnected from server')

The logger message 'Created socketio client' is printed out but it does not show the 'connected to server' message. However, during manual closing of the server connection, the 'disconnect' callback is activated... What went wrong here? I am using a nginx proxy here fyi.

Upvotes: 1

Views: 8627

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67547

Well, for starters you are connecting before the handlers are defined. You are also not doing anything after the connection. Try this version instead:

import socketio

sio = socketio.Client()
logger.info('Created socketio client')

@sio.event
def connect():
    logger.info('connected to server')

@sio.event
def disconnect():
    logger.info('disconnected from server')

sio.connect('https://server.com/socket.io/')
sio.wait()

The wait() call at the end ensures that the main thread blocks until the client is disconnected.

Upvotes: 2

Related Questions