Flask-SocketIO sid 和命名空间问题
Flask-SocketIO trouble with sid and namespaces
Flask-SocketIO 给我带来了一些麻烦。我正在尝试使用以下代码初始化私人聊天室:
@socketio.on('join', namespace='/join')
def on_join(receiver_name):
username = session['username']
join_room(receiver_name) # start a chat room with the receiver's username
send(username + ' is now connected.', room=receiver_name)
不幸的是,我收到了诸如 AttributeError: 'Request' object has no attribute 'sid'
之类的错误。即使当我特别指定 request.sid
时,我也会收到错误消息。当我为 join_room
中的 sid
字段插入一些随机内容时,我会收到命名空间错误。我已确保我正在导入所有必要的东西等。
这是具体的错误信息。
[2017-10-04 10:02:48,297] ERROR in app: Exception on /start_chat [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/agaidis/Desktop/challenge-backend-master/challenge-eng-base-master/backend-python/app.py", line 132, in start_chat
on_join(friend_username)
File "/Users/agaidis/Desktop/challenge-backend-master/challenge-eng-base-master/backend-python/app.py", line 152, in on_join
join_room(receiver_name) # start a chat room with the receiver's username
File "/usr/local/lib/python3.6/site-packages/flask_socketio/__init__.py", line 756, in join_room
sid = sid or flask.request.sid
File "/usr/local/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'sid'
127.0.0.1 - - [04/Oct/2017 10:02:48] "POST /start_chat HTTP/1.1" 500 -
您正在从 HTTP 路由中自行调用 on_join()
函数。这不是应该如何工作的。当客户端对该事件调用 emit()
时,将调用 Socket.IO 事件。 request.sid
变量仅存在于 Socket.IO 事件处理程序的上下文中,不会出现在 HTTP 处理程序中。
根据您的堆栈跟踪,我认为您需要将 start_chat
路由从 HTTP 更改为 Socket.IO。客户端需要通过 emit()
调用而不是发送 HTTP 请求来请求加入聊天。
Flask-SocketIO 给我带来了一些麻烦。我正在尝试使用以下代码初始化私人聊天室:
@socketio.on('join', namespace='/join')
def on_join(receiver_name):
username = session['username']
join_room(receiver_name) # start a chat room with the receiver's username
send(username + ' is now connected.', room=receiver_name)
不幸的是,我收到了诸如 AttributeError: 'Request' object has no attribute 'sid'
之类的错误。即使当我特别指定 request.sid
时,我也会收到错误消息。当我为 join_room
中的 sid
字段插入一些随机内容时,我会收到命名空间错误。我已确保我正在导入所有必要的东西等。
这是具体的错误信息。
[2017-10-04 10:02:48,297] ERROR in app: Exception on /start_chat [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/agaidis/Desktop/challenge-backend-master/challenge-eng-base-master/backend-python/app.py", line 132, in start_chat
on_join(friend_username)
File "/Users/agaidis/Desktop/challenge-backend-master/challenge-eng-base-master/backend-python/app.py", line 152, in on_join
join_room(receiver_name) # start a chat room with the receiver's username
File "/usr/local/lib/python3.6/site-packages/flask_socketio/__init__.py", line 756, in join_room
sid = sid or flask.request.sid
File "/usr/local/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'sid'
127.0.0.1 - - [04/Oct/2017 10:02:48] "POST /start_chat HTTP/1.1" 500 -
您正在从 HTTP 路由中自行调用 on_join()
函数。这不是应该如何工作的。当客户端对该事件调用 emit()
时,将调用 Socket.IO 事件。 request.sid
变量仅存在于 Socket.IO 事件处理程序的上下文中,不会出现在 HTTP 处理程序中。
根据您的堆栈跟踪,我认为您需要将 start_chat
路由从 HTTP 更改为 Socket.IO。客户端需要通过 emit()
调用而不是发送 HTTP 请求来请求加入聊天。