简体   繁体   中英

Setting up dynamic chat rooms in FlaskSocketIO chat

I want to let user's of my mini chat to create their rooms dynamically. For now i got const in flask server app:

# Predefined rooms for chat
ROOMS = ["Lounge", "news", "games", "coding", "food", "cars"]

And user join/leave routes are in flask:

@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'])

And on js. side its like:

// 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});

How i can let users, to join rooms they creating dynamicly?

Maybe you could do something like this for your server-side:

@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)

and client-side:

// 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});
    }

I have not tested this code, so please forgive me if there is a syntax error, or it just doesn't work, but I believe it should.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM