Improve UX with better prompt & message display

- UPDATE: last condition for messages passing the commands case and
the string.stripping()
 - FIX: clean the prompt and std.out a bit messy since broadcasting
This commit is contained in:
Fred Z 2018-03-05 16:16:56 +01:00
parent e43f19c2ae
commit 0074e57612
3 changed files with 9 additions and 6 deletions

View File

@ -10,6 +10,6 @@
- [x] ~~client freeze when sending empty string or spaces~~
- [x] ~~move closing connection at the script's end~~
- [x] ~~asking/using client-nickname~~
- [ ] clean the prompt and std.out a bit messy since broadcasting
- [x] ~~clean the prompt and std.out a bit messy since broadcasting~~
- [ ] using wlist with select.select for hardening the script
- [ ] …

View File

@ -23,7 +23,7 @@ MSG_SERVER_CONNECTED = "Serveur connecté @{}:{}"
MSG_CLOSE_CONNECTION = "Connexion vers [{}:{}] fermée"
def prompt():
sys.stdout.write('\n[me]:')
sys.stdout.write('~')
sys.stdout.flush()
def handler(signum, frame):
@ -64,5 +64,6 @@ while 1:
else: # sending message
msg = sys.stdin.readline().encode()
SERVER_CONNECTION.send(msg)
prompt()
SERVER_CONNECTION.close()

View File

@ -18,6 +18,7 @@ MSG_SERVER_STOP = "Server stop"
MSG_START_SERVER = "Server is running, listening on port {}.\n\tPress <ctrl+c> to stop"
MSG_USER_IN = "<entered the chatroom>"
MSG_WELCOME = "Welcome. First do something usefull and type your name: ".encode()
MSG_SALUTE = "Hi, {}, everyone is listening to you:\n"
inputs = []
user_name = []
@ -40,7 +41,7 @@ signal.signal(signal.SIGINT, handler)
def broadcast(sender, name, message):
# send message to all clients, except the sender
message = "\n~{}~ {}".format(name, message)
message = "{}~ {}\n".format(name, message)
for socket in inputs:
if socket != MAIN_CONNECTION and socket != sender:
try:
@ -88,7 +89,7 @@ while 1:
elif user_name[s_idx] is False: # setting username
# insert username naming rule here
user_name[s_idx] = data
socket.send("Hi, {}".format(user_name[s_idx]).encode())
socket.send(MSG_SALUTE.format(user_name[s_idx]).encode())
print(SERVER_LOG.format(*peername, name=data, msg="set user name"))
broadcast(socket, user_name[s_idx], MSG_USER_IN)
@ -97,5 +98,6 @@ while 1:
broadcast(socket, uname, data)
else:
print("Server encountered an unknown case")
import pdb; pdb.set_trace()
msg = "uncommon transmission:«{}»".format(data)
print(SERVER_LOG.format(*peername, name=uname, msg=msg))
socket.send(("server do not transmit: {}\n".format(msg)).encode())