forked from xysun/pychat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pychat_server.py
38 lines (31 loc) · 1.16 KB
/
pychat_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
# implementing 3-tier structure: Hall --> Room --> Clients;
# 14-Jun-2013
import select, socket, sys, pdb
from pychat_util import Hall, Room, Player
import pychat_util
READ_BUFFER = 4096
host = sys.argv[1] if len(sys.argv) >= 2 else ''
listen_sock = pychat_util.create_socket((host, pychat_util.PORT))
hall = Hall()
connection_list = []
connection_list.append(listen_sock)
while True:
# Player.fileno()
read_players, write_players, error_sockets = select.select(connection_list, [], [])
for player in read_players:
if player is listen_sock: # new connection, player is a socket
new_socket, add = player.accept()
new_player = Player(new_socket)
connection_list.append(new_player)
hall.welcome_new(new_player)
else: # new message
msg = player.socket.recv(READ_BUFFER)
if msg:
msg = msg.decode().lower()
hall.handle_msg(player, msg)
else:
player.socket.close()
connection_list.remove(player)
for sock in error_sockets: # close error sockets
sock.close()
connection_list.remove(sock)