For python-socketio v5, there is an error when trying to enter a client into a room when the namespace parameter is non-default.
Following are the server and client code used to isolate this situation:
Server
import socketio
from aiohttp import web
sio = socketio.AsyncServer(async_mode='aiohttp')
app = web.Application()
sio.attach(app)
@sio.event
async def connect(sid, environ):
print("connect ", sid)
sio.enter_room(sid=sid, room='test_room', namespace='/test_ns')
await sio.emit('message', data='ping', room='test_room', namespace='/test_ns')
if __name__ == '__main__':
web.run_app(app=app, port=6000)
Client
import asyncio
import socketio
sio = socketio.AsyncClient()
@sio.event
async def connect():
print('connection established')
@sio.event(namespace='/test_ns')
async def message(data):
print(data)
async def main():
await sio.connect('http://localhost:6000')
await sio.wait()
if __name__ == '__main__':
asyncio.run(main())
Traceback eventually ends up here:
File "[redacted]\venv\lib\site-packages\socketio\base_manager.py", line 115, in enter_room eio_sid = self.rooms[namespace][None][sid] KeyError: None
Probable Cause
I believe the error is caused by two functions.
- The enter_room() function in socketio/server.py (line 389 as of time of writing), which calls enter_room() in base_manager, does not have an input for eio_sid. This causes eio_sid in the enter_room() function in base_manager to always default to None.
- This means that lines 114-115 is always triggered:
if eio_sid is None:
eio_sid = self.rooms[namespace][None][sid]
In the example, the namespace to be accessed would be '/test_ns', which does not exist self.rooms, thus causing the KeyError.
The Fix
I believe that the namespace to be accessed when eio_sid is None should be '/'. Replacing eio_sid = self.rooms[namespace][None][sid] with eio_sid = self.rooms['/'][None][sid] resolved the issue.
For python-socketio v5, there is an error when trying to enter a client into a room when the namespace parameter is non-default.
Following are the server and client code used to isolate this situation:
Server
Client
Traceback eventually ends up here:
File "[redacted]\venv\lib\site-packages\socketio\base_manager.py", line 115, in enter_room eio_sid = self.rooms[namespace][None][sid] KeyError: NoneProbable Cause
I believe the error is caused by two functions.
In the example, the namespace to be accessed would be '/test_ns', which does not exist self.rooms, thus causing the KeyError.
The Fix
I believe that the namespace to be accessed when
eio_sid is Noneshould be '/'. Replacingeio_sid = self.rooms[namespace][None][sid]witheio_sid = self.rooms['/'][None][sid]resolved the issue.