-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
40 lines (29 loc) · 928 Bytes
/
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
# run this in the terminal to start server
# >>>>>> gunicorn --threads 50 server:app
# ----------------------------------
# the code about uses gunicorn to start the server
# it uses 50 threads
# nameOfFile : the app variable
import socketio
sio = socketio.Server()
app = socketio.WSGIApp(sio)
players = []
@sio.event
def connect(sid, environ):
print("A New Player Connected: ", sid)
if len(players) == 0:
sio.emit("position", "Player1", room=sid)
if len(players) == 1:
sio.emit("position", "Player2", room=sid)
players.append(sid)
sio.enter_room(sid, "game room")
print("A new player entered game room")
@sio.event
def move(sid, data):
print("MOVE from client: ", data)
sio.emit("receive", data, room="game room", skip_sid=sid)
@sio.event
def disconnect(sid):
print("Disconnected SID >> ", sid)
players.remove(sid)
print("This is now the list: ", players)