使用 Flask-SocketIO 5.0.1 加入房间时出现 TypeError
TypeError while joining a room with Flask-SocketIO 5.0.1
我正在尝试使用 Flask 在 Python 中设置一个网络应用程序,为不同的用户提供多个房间,但是使用 Flask-SocketIO 提供的 join_room
并执行脚本时出现此错误返回:
Exception in thread Thread-10:
Traceback (most recent call last):
File "D:\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "D:\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "D:\Python\Python39\lib\site-packages\socketio\server.py", line 688, in _handle_event_internal
r = server._trigger_event(data[0], namespace, sid, *data[1:])
File "D:\Python\Python39\lib\site-packages\socketio\server.py", line 712, in _trigger_event
return self.handlers[namespace][event](*args)
File "D:\Python\Python39\lib\site-packages\flask_socketio\__init__.py", line 283, in _handler
return self._handle_event(handler, message, namespace, sid,
File "D:\Python\Python39\lib\site-packages\flask_socketio\__init__.py", line 751, in _handle_event
ret = handler(*args)
File "D:\master-thesis\safety-detector\server.py", line 30, in join_room
join_room(roomId)
File "D:\master-thesis\safety-detector\server.py", line 28, in join_room
roomId = data['roomId']
TypeError: string indices must be integers
如果我注释掉 join_room(roomId)
,camId
的分配将按预期工作,所以我不知道为什么会出现此错误。
后端代码:
@socketio.on('connect')
def connection():
@socketio.on('join-room')
def join_room(data):
roomId = data['roomId']
camId = data['camId']
join_room(roomId)
emit('cam-connected', {'camId': camId}, broadcast=True)
@socketio.on('disconnect')
def on_disconnect():
leave_room(roomId)
emit('cam-disconnected', {'camId': camId}, broadcast=True)
您的代码中有一个名为 join_room()
的函数,它隐藏了 Flask-SocketIO 中的 join_room()
函数。
您的 Socket.IO 处理程序的结构也很奇怪,内部函数不太可能正常工作(或者当您 copy/pasted 您问题中的代码时缩进弄乱了? ).尝试这样的事情:
@socketio.on('connect')
def connection():
pass
@socketio.on('join-room')
def my_join_room(data): # <--- rename this to something other than join_room
roomId = data['roomId']
camId = data['camId']
join_room(roomId)
emit('cam-connected', {'camId': camId}, broadcast=True)
@socketio.on('disconnect')
def on_disconnect():
leave_room(roomId)
emit('cam-disconnected', {'camId': camId}, broadcast=True)
我正在尝试使用 Flask 在 Python 中设置一个网络应用程序,为不同的用户提供多个房间,但是使用 Flask-SocketIO 提供的 join_room
并执行脚本时出现此错误返回:
Exception in thread Thread-10:
Traceback (most recent call last):
File "D:\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "D:\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "D:\Python\Python39\lib\site-packages\socketio\server.py", line 688, in _handle_event_internal
r = server._trigger_event(data[0], namespace, sid, *data[1:])
File "D:\Python\Python39\lib\site-packages\socketio\server.py", line 712, in _trigger_event
return self.handlers[namespace][event](*args)
File "D:\Python\Python39\lib\site-packages\flask_socketio\__init__.py", line 283, in _handler
return self._handle_event(handler, message, namespace, sid,
File "D:\Python\Python39\lib\site-packages\flask_socketio\__init__.py", line 751, in _handle_event
ret = handler(*args)
File "D:\master-thesis\safety-detector\server.py", line 30, in join_room
join_room(roomId)
File "D:\master-thesis\safety-detector\server.py", line 28, in join_room
roomId = data['roomId']
TypeError: string indices must be integers
如果我注释掉 join_room(roomId)
,camId
的分配将按预期工作,所以我不知道为什么会出现此错误。
后端代码:
@socketio.on('connect')
def connection():
@socketio.on('join-room')
def join_room(data):
roomId = data['roomId']
camId = data['camId']
join_room(roomId)
emit('cam-connected', {'camId': camId}, broadcast=True)
@socketio.on('disconnect')
def on_disconnect():
leave_room(roomId)
emit('cam-disconnected', {'camId': camId}, broadcast=True)
您的代码中有一个名为 join_room()
的函数,它隐藏了 Flask-SocketIO 中的 join_room()
函数。
您的 Socket.IO 处理程序的结构也很奇怪,内部函数不太可能正常工作(或者当您 copy/pasted 您问题中的代码时缩进弄乱了? ).尝试这样的事情:
@socketio.on('connect')
def connection():
pass
@socketio.on('join-room')
def my_join_room(data): # <--- rename this to something other than join_room
roomId = data['roomId']
camId = data['camId']
join_room(roomId)
emit('cam-connected', {'camId': camId}, broadcast=True)
@socketio.on('disconnect')
def on_disconnect():
leave_room(roomId)
emit('cam-disconnected', {'camId': camId}, broadcast=True)