Refuses connection if another session is already active

This commit is contained in:
Saptak S 2021-11-14 23:44:16 +05:30
parent 6429392a40
commit b9d9f14916
No known key found for this signature in database
GPG Key ID: 7B7F1772C0C6FCBF

View File

@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from flask import request, render_template, make_response, jsonify, session from flask import request, render_template, make_response, jsonify, session
from flask_socketio import emit from flask_socketio import emit, ConnectionRefusedError
class ChatModeWeb: class ChatModeWeb:
@ -117,17 +117,20 @@ class ChatModeWeb:
def server_connect(): def server_connect():
"""Sent by clients when they enter a room. """Sent by clients when they enter a room.
A status message is broadcast to all people in the room.""" A status message is broadcast to all people in the room."""
self.connected_users.append(session.get("name")) if self.validate_username(session.get("name")):
emit( self.connected_users.append(session.get("name"))
"status", emit(
{ "status",
"username": session.get("name"), {
"msg": "{} has joined.".format(session.get("name")), "username": session.get("name"),
"connected_users": self.connected_users, "msg": "{} has joined.".format(session.get("name")),
"user": session.get("name"), "connected_users": self.connected_users,
}, "user": session.get("name"),
broadcast=True, },
) broadcast=True,
)
else:
raise ConnectionRefusedError('You are active from another session!')
@self.web.socketio.on("text", namespace="/chat") @self.web.socketio.on("text", namespace="/chat")
def text(message): def text(message):