-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
61 lines (47 loc) · 2.25 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import socket
import threading
import json
from client import Client
class ChatterServer:
"""A class to run the Chatter server"""
def __init__(self):
self.socket = socket.socket()
self.host = '127.0.0.1'
self.port = 10000
self.clients = {}
def run(self):
# Start socket and bind
try:
self.socket.bind((self.host, self.port))
print('Chatter server is listening...')
self.socket.listen(5)
# Process client connections
while True:
client, address = self.socket.accept()
print('Connected to: ' + address[0] + ':' + str(address[1]))
threading.Thread(target=self.on_client_connect, args=(client,), daemon=True).start()
except socket.error as e:
print(str(e))
def on_client_connect(self, connection):
while True:
# Decode
message = connection.recv(2048).decode('utf-8')
decoded_message = json.loads(message)
# Keep track of clients
if decoded_message["type"] == "connect":
self.clients[decoded_message["client_id"]] = Client(decoded_message["client_id"], decoded_message["display_name"], connection)
# Send updated friend list to all OTHER clients
friend_list = {client_id: client.display_name for client_id, client in self.clients.items()}
friend_message = {"type": "friends", "friends": friend_list}
encoded_message = json.dumps(friend_message, ensure_ascii=False)
self.send_all_clients(decoded_message.get("client_id"), encoded_message, True)
if decoded_message["type"] == "room":
# Send to all OTHER clients
self.send_all_clients(decoded_message.get("client_id"), message)
def send_all_clients(self, from_client_id, message, send_self = False):
for client in self.clients.values():
if send_self or client.id != from_client_id:
client.connection.sendall(message.encode('utf-8'))
if __name__ == '__main__':
chatter = ChatterServer()
chatter.run()