S
S
Sazoks2021-03-24 10:54:13
Flask
Sazoks, 2021-03-24 10:54:13

How to remove a client from a room when a socket is disconnected?

Good day!
There is a standard function to disconnect a client from a room:

def __client_leave_room(self, data):
        """
        Метод отключения клиента от комнаты.
        :param data: Данные пользователя. Приходят с клиента.
        :return:
        """

        username = data["username"]
        name_room = data["room"]

        if name_room not in self.__list_rooms:
            # Ошибка, такой комнаты нет.
            pass

        leave_room(name_room)
        print(f"{username}'s disconnected from {name_room}")


I forgot to say that this is not a function at all, but a method in a class. He is a worker.

The problem is that it works well when the user presses the "Disconnect" button on the client, but if he closes, refreshes or leaves the page, then this function does not work, although I wrote this on the client:

socket.on("disconnect", function() {
    socket.emit("leave", {username: username, room: room});
});


Thanks in advance for your replies!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-03-24
@bacon

Most likely, you need to catch the on_disconnect event on the server or the like, which is represented by the library

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question