From b742bf42ad2de59bf7c4c3af39c3f304e0d5bc8e Mon Sep 17 00:00:00 2001 From: Fred Z Date: Wed, 28 Feb 2018 11:29:18 +0100 Subject: [PATCH] Add broadcasting feature Now it's a chat-room, each message sending by a client is now broadcasted to other clients. Modifications on the client are: - rename connections list as in server.py - fix using the wrong list for checking events. It was not a problem before implementation of broadcasting --- chat/TODO.md | 2 +- chat/client.py | 8 ++++---- chat/server.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/chat/TODO.md b/chat/TODO.md index f8d8db0..00da139 100644 --- a/chat/TODO.md +++ b/chat/TODO.md @@ -4,7 +4,7 @@ - [x] ~~clean client exit with ~~ - [x] ~~clean server exit with ~~ - [ ] showing message on server when client disconnect -- [ ] broadcasting messages to all client connected +- [x] ~~broadcasting messages to all client connected~~ - [ ] sending welcome message only at 1st client connection - [ ] asking/using client-nickname - [ ] client freeze when sending empty string or spaces diff --git a/chat/client.py b/chat/client.py index 688d63a..70cbb4b 100755 --- a/chat/client.py +++ b/chat/client.py @@ -23,7 +23,7 @@ MSG_SERVER_CONNECTED = "Serveur connecté @{}:{}" MSG_CLOSE_CONNECTION = "\nConnexion vers [{}:{}] fermée" def prompt(): - sys.stdout.write('[me] ') + sys.stdout.write('\n[me]:') sys.stdout.flush() def handler(signum, frame): @@ -43,10 +43,10 @@ except ConnectionRefusedError as except_detail: print(MSG_SERVER_CONNECTED.format(HOST, PORT)) while 1: - sockets_list = [sys.stdin, SERVER_CONNECTION] - rlist, wlist, elist = select.select(sockets_list, [], []) + inputs = [sys.stdin, SERVER_CONNECTION] + rlist, wlist, elist = select.select(inputs, [], []) - for socket in sockets_list: + for socket in rlist: if socket == SERVER_CONNECTION: # incomming message data = socket.recv(BUFFER).decode() if not data: diff --git a/chat/server.py b/chat/server.py index a9d65e9..737c7c8 100755 --- a/chat/server.py +++ b/chat/server.py @@ -36,6 +36,17 @@ def handler(signum, frame): signal.signal(signal.SIGINT, handler) +def broadcast(sender, message): + # send message to all clients, except the sender + for socket in inputs: + if socket != MAIN_CONNECTION and socket != sender: + try: + message = "\n" + message + socket.send(message.encode()) + except : + socket.close() + inputs.remove(socket) + # Creation de la connection MAIN_CONNECTION = socket.socket(socket.AF_INET, socket.SOCK_STREAM) MAIN_CONNECTION.bind((HOST, PORT)) @@ -54,18 +65,22 @@ while 1: socket_object, socket_addr = socket.accept() inputs.append(socket_object) print(MSG_NEW_CLIENT.format(socket_addr)) + broadcast(socket, MSG_NEW_CLIENT.format(socket_addr)) else: # receiving data try: data = socket.recv(BUFFER).decode().strip() if data.upper() == "QUIT": print(MSG_CLIENT_DISCONNECTED.format(socket.getpeername())) + broadcast(socket, + MSG_CLIENT_DISCONNECTED.format(socket.getpeername())) inputs.remove(socket) socket.close() continue elif data: print(MSG_CLIENT_ID.format(socket.getpeername(), data)) + broadcast(socket, data) socket.send(MSG_WELCOME) except Exception as except_detail: print("Exception: «{}»".format(except_detail))