diff --git a/chatserver/server.py b/chatserver/server.py index ca5a6d5..cbc42e4 100644 --- a/chatserver/server.py +++ b/chatserver/server.py @@ -131,10 +131,16 @@ def handle_disconnect(): @socketio.on('join') def handle_join(data): + print(f"Received join event: {data}") username = data.get('username') chat_room = data.get('chat_room', 'default') + + if not username: + emit('error', {'message': 'Username is required'}) + return + join_room(chat_room) - emit('message', {'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) @socketio.on('leave') def handle_leave(data): @@ -145,10 +151,15 @@ def handle_leave(data): @socketio.on('send_message') def handle_send_message(data): + print(f"Received send_message event: {data}") username = data.get('username') chat_room = data.get('chat_room', 'default') message = data.get('message') + if not username or not message: + emit('error', {'message': 'Username and message are required'}) + return + # Store the message in the chat room's history if chat_room not in message_history: message_history[chat_room] = [] diff --git a/chatserver/templates/index.html b/chatserver/templates/index.html index 6bb20fc..64f0841 100644 --- a/chatserver/templates/index.html +++ b/chatserver/templates/index.html @@ -50,6 +50,8 @@ let username = prompt("Enter your username:"); let chatRoom = prompt("Enter chat room (default: 'default')") || 'default'; + // Log the join event data + console.log('Joining chat room:', { username: username, chat_room: chatRoom }); // Join the chat room socket.emit('join', { username: username, chat_room: chatRoom }); @@ -65,10 +67,17 @@ function sendMessage() { const message = messageInput.value; if (message.trim() !== '') { + // Log the send_message event data + console.log('Sending message:', { username: username, chat_room: chatRoom, message: message }); socket.emit('send_message', { username: username, chat_room: chatRoom, message: message }); messageInput.value = ''; // Clear the input field } } + + // Log the send_message event data + console.log('Sending message:', { username: username, chat_room: chatRoom, message: message }); + // Send a message to the server + socket.emit('send_message', { username: username, chat_room: chatRoom, message: message }); \ No newline at end of file