Flask-SocketIO 无法正常加入指定房间 rooms
Hi. 我想用 Flask-SocketIO
来构建一个聊天网站……但是我却无法让用户加入指定房间:重新请求一次的话用户就不在那个房间里了。。
代码:
from flask_socketio import emit, join_room, leave_room, roomsfrom ..extensions import socketio
from ..schemas import UserSchema
from ..models import User
user_schema = UserSchema()
# ...
@socketio.on("chat")
def handle_chat(msg):
evt = msg["event"] if "event" in msg else "users"
print(rooms())
match evt:
case "join":
user = msg["user"]
room = msg["room"]
join_room(room["name"])
print(room["name"])
socketio.emit("chat", {
"type": "newuser",
"message": f"Welcome {user['username']} to {room['name']} !"
}, room=room["name"])
case "message":
user = msg["user"]
room = msg["room"]
socketio.emit("chat", {
"type": "message",
"message": msg["msg"]
}, room=room["name"])
console 输出:
['TzlqpgeT9B-jBnafAAAD', '测试群聊'] # Joined the user to room测试群聊 # room name
['tjiHpwIS5xeIli8UAAAB'] # and when I send another request, room resets
谢谢!!
以上是 Flask-SocketIO 无法正常加入指定房间 rooms 的全部内容, 来源链接: utcz.com/p/938317.html