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
This commit is contained in:
parent
7ecefcf2ad
commit
b742bf42ad
|
@ -4,7 +4,7 @@
|
|||
- [x] ~~clean client exit with <ctrl-c>~~
|
||||
- [x] ~~clean server exit with <ctrl-c>~~
|
||||
- [ ] 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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue