在 Flask Socket IO 聊天中设置动态聊天室
Posted
技术标签:
【中文标题】在 Flask Socket IO 聊天中设置动态聊天室【英文标题】:Setting up dynamic chat rooms in FlaskSocketIO chat 【发布时间】:2019-12-08 15:58:38 【问题描述】:我想让我的迷你聊天的用户动态地创建他们的房间。现在我在烧瓶服务器应用程序中得到了 const:
# Predefined rooms for chat
ROOMS = ["Lounge", "news", "games", "coding", "food", "cars"]
并且用户加入/离开路线在烧瓶中:
@socketio.on('join')
def on_join(data):
username = data["username"]
room = data["room"]
join_room(room)
# Notofication about new user joined room
send("msg": username + " has joined the " + room + " room.", room=room)
@socketio.on('leave')
def on_leave(data):
leave_room(data['room'])
# Notification about leaving users
send('msg': data['username'] + " has left the " + data['room'], room=data['room'])
在 js 上。喜欢它:
// Select a room
document.querySelectorAll('.select-room').forEach(p =>
p.onclick = () =>
let newRoom = p.innerhtml
// Check if user already in the room
if (newRoom === room)
msg = `You are already in $room room.`;
printSysMsg(msg);
else
leaveRoom(room);
joinRoom(newRoom);
room = newRoom;
;
);
// Trigger 'join' event
function joinRoom(room)
// Join room
socket.emit('join', 'username': username, 'room': room);
// Trigger 'leave' event if user was previously on a room
function leaveRoom(room)
socket.emit('leave', 'username': username, 'room': room);
我如何让用户加入他们动态创建的房间?
【问题讨论】:
这是选择自定义房间所需的 javascript UI 更改的问题,还是 Socket.IO 服务器特有的问题? @Miguel 我认为在两者中,我都不知道如何让用户在两侧创建动态房间 我能回答的部分是Socket.IO端。这很简单,只需使用所需的房间名称即可。房间是在第一次使用时创建的,因此创建房间所需要做的唯一事情就是让某人加入。 我认为你的函数缺少一个右花括号...我希望在你的真实代码中不是这样。 【参考方案1】:也许你可以为你的服务器端做这样的事情:
@socketio.on('join')
def on_join(data):
username = data["username"]
room = data["room"]
join_room(room)
# Notofication about new user joined room
send("msg": username + " has joined the " + room + " room.", room=room)
@socketio.on('leave')
def on_leave(data):
leave_room(data['room'])
# Notification about leaving users
send('msg': data['username'] + " has left the " + data['room'], room=data['room'])
@socketio.on('new_room')
def new_room(data):
ROOMS.append(data['new_room_name'])
room = data['new_room_name']
username = data['username']
join_room(data['new_room_name'])
# Notification about new user joined room
send("msg": username + " has created the " + room + " room.", room=room)
和客户端:
// Select a room
document.querySelectorAll('.select-room').forEach(p =>
p.onclick = () =>
let newRoom = p.innerHTML
// Check if user already in the room
if (newRoom === room)
msg = `You are already in $room room.`;
printSysMsg(msg);
else
leaveRoom(room);
joinRoom(newRoom);
room = newRoom;
;
);
// Trigger 'join' event
function joinRoom(room)
// Join room
socket.emit('join', 'username': username, 'room': room);
// Trigger 'leave' event if user was previously on a room
function leaveRoom(room)
socket.emit('leave', 'username': username, 'room': room);
// Trigger this event when a new room needs to be created
function newRoom(room)
socket.emit('new_room', 'username': username, 'room': room);
我没有测试过这段代码,所以如果有语法错误,请原谅我,或者它只是不起作用,但我相信它应该。
【讨论】:
以上是关于在 Flask Socket IO 聊天中设置动态聊天室的主要内容,如果未能解决你的问题,请参考以下文章
将简单的 Socket.io Flask 应用程序部署到 Google App Engine:仍未解决