Create the client, and modify the server.

This commit is contained in:
Charles Descamps 2025-03-13 16:05:26 -07:00
parent 5443d01d5c
commit 12eb4fb3f5
2 changed files with 53 additions and 6 deletions

View file

@ -48,7 +48,7 @@ def main():
print("\nAccepted connection from new client.")
client_socket.send(b"\nWelcome to the chat server!\n")
client_socket.send(b"\nUse '%!' to exit, or '%?' for help.\n")
client_socket.send(b"\nType in a username: \n")
client_socket.send(b"\nType in a username: ")
client_states[client_socket] = "WAITING_FOR_USERNAME"
# otherwise the socket is a client socket.
else:
@ -60,17 +60,17 @@ def main():
if state == "WAITING_FOR_USERNAME":
usernames[sock] = message.strip()
chat_rooms[sock] = "default"
sock.send(b"\nUsername accepted. You are now in the 'default' chatroom.\n")
sock.send(b"\nUsername accepted. You are now in the 'default' chatroom.")
client_states[sock] = "NORMAL"
elif state == "WAITING_FOR_NEW_USERNAME":
usernames[sock] = message.strip()
sock.send(b"\nUsername changed successfully.\n")
sock.send(b"\nUsername changed successfully.")
print(f"\nClient switched username to {usernames[sock]}.")
client_states[sock] = "NORMAL"
elif state == "WAITING_FOR_NEW_CHATROOM":
old_chatroom = chat_rooms[sock]
chat_rooms[sock] = message.strip()
sock.send(f"\nSwitched to chatroom '{chat_rooms[sock]}.'\n".encode('utf-8'))
sock.send(f"\nSwitched to chatroom '{chat_rooms[sock]}.'".encode('utf-8'))
print(f"\nClient switched to chatroom '{chat_rooms[sock]}.'")
if old_chatroom != "default" and old_chatroom not in chat_rooms.values():
print(f"\nChatroom '{old_chatroom}' is empty and removed.")
@ -93,10 +93,10 @@ def main():
sock.send(b"\t%* : Switch to another chatroom.\n")
sock.send(b"\t%$ : Change Username.\n")
elif message.strip() == "%*":
sock.send(b"\nEnter chatroom name: \n")
sock.send(b"\nEnter chatroom name: ")
client_states[sock] = "WAITING_FOR_NEW_CHATROOM"
elif message.strip() == "%$":
sock.send(b"\nEnter new username: \n")
sock.send(b"\nEnter new username: ")
client_states[sock] = "WAITING_FOR_NEW_USERNAME"
else:
username = usernames[sock]