Modify the server to allow for universal message handling.
This commit is contained in:
parent
8f9f786b23
commit
093da92186
1 changed files with 22 additions and 27 deletions
|
|
@ -68,6 +68,22 @@ def get_client_uuid(client_socket, uuid_dict):
|
||||||
|
|
||||||
return user_id
|
return user_id
|
||||||
|
|
||||||
|
def handle_message(username, chat_room, message):
|
||||||
|
"""Store the message in history and broadcast it to all clients."""
|
||||||
|
# Store the message in the chat room's history
|
||||||
|
if chat_room not in message_history:
|
||||||
|
message_history[chat_room] = []
|
||||||
|
message_history[chat_room].append(f"{username}: {message}")
|
||||||
|
|
||||||
|
# Broadcast the message to WebSocket clients in the chat room
|
||||||
|
socketio.emit('message', {'username': username, 'message': message}, room=chat_room)
|
||||||
|
|
||||||
|
# Broadcast the message to socket-based clients in the chat room
|
||||||
|
send_message = f"{username} >> {message}".encode('utf-8')
|
||||||
|
for client_sock in socket_list:
|
||||||
|
if client_sock != server_socket and chat_rooms.get(client_sock) == chat_room:
|
||||||
|
client_sock.send(send_message)
|
||||||
|
|
||||||
# REST API Endpoints
|
# REST API Endpoints
|
||||||
@app.route('/send_message', methods=['POST'])
|
@app.route('/send_message', methods=['POST'])
|
||||||
def send_message():
|
def send_message():
|
||||||
|
|
@ -80,16 +96,8 @@ def send_message():
|
||||||
if not username or not message:
|
if not username or not message:
|
||||||
return jsonify({"error": "Username and message are required"}), 400
|
return jsonify({"error": "Username and message are required"}), 400
|
||||||
|
|
||||||
# Store the message in the chat room's history
|
# Use the centralized message handling function
|
||||||
if chat_room not in message_history:
|
handle_message(username, chat_room, message)
|
||||||
message_history[chat_room] = []
|
|
||||||
message_history[chat_room].append(f"{username}: {message}")
|
|
||||||
|
|
||||||
# Broadcast the message to all clients in the same chat room
|
|
||||||
send_message = f"{username} >> {message}".encode('utf-8')
|
|
||||||
for client_sock in socket_list:
|
|
||||||
if client_sock != server_socket and chat_rooms.get(client_sock) == chat_room:
|
|
||||||
client_sock.send(send_message)
|
|
||||||
|
|
||||||
return jsonify({"status": "Message sent and saved in history"}), 200
|
return jsonify({"status": "Message sent and saved in history"}), 200
|
||||||
|
|
||||||
|
|
@ -160,13 +168,8 @@ def handle_send_message(data):
|
||||||
emit('error', {'message': 'Username and message are required'})
|
emit('error', {'message': 'Username and message are required'})
|
||||||
return
|
return
|
||||||
|
|
||||||
# Store the message in the chat room's history
|
# Use the centralized message handling function
|
||||||
if chat_room not in message_history:
|
handle_message(username, chat_room, message)
|
||||||
message_history[chat_room] = []
|
|
||||||
message_history[chat_room].append(f"{username}: {message}")
|
|
||||||
|
|
||||||
# Broadcast the message to the chat room
|
|
||||||
emit('message', {'username': username, 'message': message}, room=chat_room)
|
|
||||||
|
|
||||||
# Chat server logic
|
# Chat server logic
|
||||||
def chat_server():
|
def chat_server():
|
||||||
|
|
@ -287,17 +290,9 @@ def chat_server():
|
||||||
else:
|
else:
|
||||||
username = client_info[sock]["username"]
|
username = client_info[sock]["username"]
|
||||||
chat_room = chat_rooms[sock]
|
chat_room = chat_rooms[sock]
|
||||||
send_message = f"{username} >> {message}".encode('utf-8')
|
|
||||||
|
|
||||||
# Store the message in the chat room's history
|
# Use the centralized message handling function
|
||||||
if chat_room not in message_history:
|
handle_message(username, chat_room, message)
|
||||||
message_history[chat_room] = []
|
|
||||||
message_history[chat_room].append(f"{username}: {message}")
|
|
||||||
|
|
||||||
# Broadcast the message to all clients in the same chat room
|
|
||||||
for client_sock in socket_list:
|
|
||||||
if client_sock != server_socket and chat_rooms.get(client_sock) == chat_room:
|
|
||||||
client_sock.send(send_message)
|
|
||||||
else:
|
else:
|
||||||
sock.close()
|
sock.close()
|
||||||
socket_list.remove(sock)
|
socket_list.remove(sock)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue