Server demo:
from aiohttp import web
import socketio
sio = socketio.AsyncServer(async_mode='aiohttp')
app = web.Application()
sio.attach(app)
async def index(request):
pass
@sio.on('connection')
async def on_connect():
print('client connected')
@sio.on('schedule')
async def ping(sid):
await sio.emit('schedule', {
'data': 'ok'
})
app.router.add_get('/', index)
if __name__ == '__main__':
web.run_app(app)
Client code (default transports):
import socketio
sio = socketio.Client()
@sio.on('connect')
def on_connect():
print('connected to server')
sio.emit('schedule')
@sio.on('schedule')
def on_pong(data):
print(data)
if __name__ == '__main__':
sio.connect('http://localhost:8080')
sio.wait()
When transports is default (['polling', 'websocket']), everything is fine, output is expected:
connected to server
{'data': 'ok'}
But when transports set to websocket, client exited without msg and server did not receive any msg.
sio.connect('http://localhost:8080', transports='websocket')
Hmm. The transport set to websocket works for me. Are you using the latest versions of both python-socketio and python-engineio?
You may also want to enable logging to see what's happening. On the client:
sio = socketio.Client(engineio_logger=True)
This is the output that I get here:
Attempting WebSocket connection to ws://localhost:8080/socket.io/?transport=websocket&EIO=3
WebSocket connection accepted with {'upgrades': [], 'pingInterval': 25000, 'pingTimeout': 60000, 'sid': '8af68c42863449628d3727cbc78d4c6c'}
Sending packet PING data None
Received packet MESSAGE data 0
connected to server
Sending packet MESSAGE data 2["schedule"]
Received packet PONG data None
Received packet MESSAGE data 2["schedule",{"data":"ok"}]
{'data': 'ok'}
Thanks for the quick response.
My mistake! I didn't install websocket lib...
FYI: I found https://github.com/miguelgrinberg/python-engineio/blob/master/engineio/client.py#L306 logging a warning message, but if I initialize Client without engineio_logger=True, self.logger level is ERROR, so the warning message will never show. Maybe it is a good idea that setting log level to WARNING or using self.logger.error(msg) here?
As documented, you can install the dependencies for the client if you install this package with:
pip install "python-socketio[client]"
These are not installed by default because most people use just the server.
Maybe it is a good idea that setting log level to WARNING or using self.logger.error(msg) here?
The log level does not matter if you don't have a logger attached. Maybe I'll make it an error, as you suggest.
Most helpful comment
As documented, you can install the dependencies for the client if you install this package with:
These are not installed by default because most people use just the server.
The log level does not matter if you don't have a logger attached. Maybe I'll make it an error, as you suggest.