Modify the selfhosted webclient. Tried to fix the errors? The server has been modified too to try and compensate for errors.

This commit is contained in:
Charles Descamps 2025-03-27 12:22:18 -07:00
parent dab35d9832
commit 44cdbde003
2 changed files with 21 additions and 1 deletions

View file

@ -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] = []

View file

@ -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 });
</script>
</body>
</html>