-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignaling_server.py
46 lines (39 loc) · 1.45 KB
/
signaling_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
import socket
import threading
# Dictionary to store chat_id and (host, port) of the chat creator
chat_rooms = {}
def handle_client(client_socket):
while True:
try:
message = client_socket.recv(1024).decode()
if not message:
break
command, chat_id, host, port = message.split()
if command == "REGISTER":
chat_rooms[chat_id] = (host, int(port))
client_socket.send("REGISTERED".encode())
elif command == "GET":
if chat_id in chat_rooms:
creator_host, creator_port = chat_rooms[chat_id]
client_socket.send(f"{creator_host} {creator_port}".encode())
else:
client_socket.send("NOT_FOUND".encode())
except:
break
client_socket.close()
def start_server():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 5555))
server.listen(5)
print("Signaling server started on port 5555")
try:
while True:
client_socket, addr = server.accept()
client_handler = threading.Thread(target=handle_client, args=(client_socket,))
client_handler.start()
except KeyboardInterrupt:
print("\nShutting down the signaling server...")
finally:
server.close()
if __name__ == "__main__":
start_server()