diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2017-03-30 22:01:50 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2017-03-30 22:01:50 +0300 |
| commit | b3ade7d924fff95a6e0e6b010d5ed783b51f435f (patch) | |
| tree | 218a8821853d26c9f22ba8aa5d18d5693f48a769 /server.py | |
| parent | 73e1db4ea519fffb5f4c23d3bc348ee5f15f14a3 (diff) | |
Add welcome message with online users
Diffstat (limited to 'server.py')
| -rwxr-xr-x | server.py | 16 |
1 files changed, 14 insertions, 2 deletions
@@ -39,6 +39,15 @@ def parse_message(text): debug("Command '{}' from '{}' with body '{}'".format(command, username, body)) return (command, username, body) +online_nicks = [] +def send_online_nicks(recipient): + msg = "Online users: " + ", ".join(online_nicks) + response = "BROADCAST\nSERVER\n{}\n".format(msg) + try: + recipient.send(response.encode()) + except OSError: + debug("ERROR: Couldn't broadcast message to {}".format(str(rec))) + def broadcast(message, username, recipients): debug("Broadcasting message '{}'".format(message)) for rec in recipients: @@ -48,11 +57,14 @@ def broadcast(message, username, recipients): except OSError: debug("ERROR: Couldn't broadcast message to {}".format(str(rec))) -def do_command(command, username, body, bc): +def do_command(command, username, body, bc, sender): if command == "CONNECT": + online_nicks.append(username) + send_online_nicks(sender) bc("User {} has connected".format(username), username) elif command == "DISCONNECT": bc("User {} has disconnected".format(username), username) + online_nicks.remove(username) elif command == "MESSAGE": bc("{}".format(body), username) elif command == "KEEPALIVE": @@ -76,7 +88,7 @@ def keep_connection(conn, addr, connections): continue command, username, body = data_tuple - do_command(command, username, body, broadcast_fun) + do_command(command, username, body, broadcast_fun, sender) conn.close() connections.remove(conn) |
