Attempt to fix the Direct Messaging capabilities for the clients.

This commit is contained in:
Charles Descamps 2025-03-28 20:09:00 -07:00
parent 66202e154b
commit e12f8497f4

View file

@ -144,24 +144,39 @@ def handle_command(username, chat_room, command, sock=None):
target_username = parts[1] target_username = parts[1]
dm_message = parts[2] dm_message = parts[2]
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# Check if the target is a socket-based client
target_sock = None target_sock = None
for s, info in client_info.items(): for s, info in client_info.items():
if info.get("username") == target_username: if not info.get("is_websocket") and info.get("username") == target_username:
target_sock = s target_sock = s
break break
# Check if the target is a WebSocket client
target_websocket_sid = None
for sid, info in client_info.items():
if info.get("is_websocket") and info.get("username") == target_username:
target_websocket_sid = sid
break
if target_sock: if target_sock:
# Send the direct message to the socket-based client
sender_username = username sender_username = username
target_sock.send(f"[{timestamp}] DM from {sender_username}: {dm_message}\n".encode('utf-8')) target_sock.send(f"[{timestamp}] DM from {sender_username}: {dm_message}\n".encode('utf-8'))
if sock: if sock:
sock.send(f"[{timestamp}] Direct message sent.\n".encode('utf-8')) sock.send(f"[{timestamp}] Direct message sent.\n".encode('utf-8'))
else: elif target_websocket_sid:
socketio.emit('message', { # Send the direct message to the WebSocket client
'username': sender_username, socketio.emit('message', {
'message': dm_message, 'username': username,
'timestamp': timestamp, 'message': dm_message,
'chat_room': 'Direct Message' 'timestamp': timestamp,
}, room=chat_room) 'chat_room': 'Direct Message'
}, to=target_websocket_sid)
if sock:
sock.send(f"[{timestamp}] Direct message sent.\n".encode('utf-8'))
else: else:
# Target user not found
error_message = "User not found." error_message = "User not found."
if sock: if sock:
sock.send(f"\n{error_message}\n".encode('utf-8')) sock.send(f"\n{error_message}\n".encode('utf-8'))
@ -237,6 +252,10 @@ def get_history():
@socketio.on('connect') @socketio.on('connect')
def handle_connect(): def handle_connect():
print("WebSocket client connected") print("WebSocket client connected")
# Assign a unique ID to the WebSocket client
user_id = str(uuid.uuid4())
client_info[request.sid] = {"user_id": user_id, "username": None, "is_websocket": True}
print(f"WebSocket client registered with ID: {user_id}")
@socketio.on('disconnect') @socketio.on('disconnect')
def handle_disconnect(): def handle_disconnect():
@ -252,6 +271,10 @@ def handle_join(data):
emit('error', {'message': 'Username is required'}) emit('error', {'message': 'Username is required'})
return return
# Update client info
client_info[request.sid]["username"] = username
client_info[request.sid]["chat_room"] = chat_room
join_room(chat_room) join_room(chat_room)
emit('message', {'username': 'Server', 'message': f"{username} has joined the chat room '{chat_room}'."}, room=chat_room) emit('message', {'username': 'Server', 'message': f"{username} has joined the chat room '{chat_room}'."}, room=chat_room)