1
Flask-SocketIO給我帶來一些麻煩。我想初始化用下面的代碼私人聊天室:Flask-SocketIO遇到sid和命名空間問題
@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 -
謝謝!我是Flask和Socket.IO的noob。爲了實例化多個房間,客戶端在客戶端代碼中打開新的套接字連接,對嗎?他們看到的網頁將是普通的HTML,其中的東西是特定於其開放套接字的? – agaidis
您可以將單個客戶端連接與多個房間相關聯,無需創建多個連接。對同一個客戶端多次調用'join_room()'完全可以。 – Miguel
太棒了!謝謝!! – agaidis