-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
186 lines (134 loc) · 3.97 KB
/
hello.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit, send
from flask_socketio import join_room, leave_room
import time
app = Flask(__name__)
#app.debug = True
socketio = SocketIO(app, cors_allowed_origins="*")
availableRooms = set()
roomStartTimes = dict()
roomLightFunc = dict()
userRoomMap = dict()
def getRoomStartTime(roomID):
if roomID in roomStartTimes:
return roomStartTimes[roomID]
roomStartTimes[roomID] = time.time()*1000
def getRoomLightFunc(roomID):
if roomID in roomLightFunc:
return roomLightFunc[roomID]
return {
'identifier': 'constantColorCanvas',
'params': {
'color': '#FF0000'
}
}
def updateRoomLightFunc(roomID, newLightFunc):
if roomExists(roomID):
roomLightFunc[roomID] = newLightFunc
roomStartTimes[roomID] = time.time()*1000
return True
return False
def roomExists(roomID):
return roomID in availableRooms
def createRoom(roomID):
availableRooms.add(roomID)
def userSwitchRoom(username, roomID):
ret = None if username not in userRoomMap else userRoomMap[username]
if username in userRoomMap and roomID is None:
del userRoomMap[username]
else:
userRoomMap[username] = roomID
return ret
@app.route('/')
def index():
return render_template('index.html')
@app.route('/roomJoin')
def roomJoin():
return render_template('roomJoin.html')
@app.route('/roomCreate')
def roomCreate():
return render_template('roomCreate.html')
@app.route('/demo')
def index1():
return render_template('demo.html')
@app.route('/demo_client')
def index2():
return render_template('demo_client.html')
@app.route('/rainbow/<string:roomName>')
def rainbow(roomName):
return render_template('rainbow.html')
"""
@app.route('/room/<string:roomName>')
def room(roomName):
print(roomName)
return render_template('index.html')
"""
@socketio.on('join')
def on_join(data):
username = request.sid
roomID = data['room']
if not roomExists(roomID):
emit('error', {'msg':'Room does not exist'})
else:
to_leave = userSwitchRoom(username,roomID)
if to_leave is not None:
leave_room(to_leave)
print(str(username) + " left room "+str(to_leave))
join_room(roomID)
print(str(username) + " joined room "+str(roomID))
toSend = {
'startTime': getRoomStartTime(roomID),
'frameCount': 0,
'lightFuncData': getRoomLightFunc(roomID)
}
emit('update', toSend)
"""
@socketio.on('connect')
def on_client_connect():
print(data)
roomID = 123;#data['room']
print("Hello")
toSend = {
'startTime': getRoomStartTime(roomID),
'frameCount': 0,
'lightFuncData': {
'identifier': 'cycleColorCanvas',
'params': {
'colors': ['#FF0000', '#00FF00', '#0000FF', '#FF00FF']
}
}
}
emit('update', toSend)
"""
@socketio.on('disconnect')
def test_disconnect():
username = request.sid
to_leave = userSwitchRoom(username,None)
if to_leave is not None:
leave_room(to_leave)
print(str(username) + " left room "+str(to_leave))
print('Client disconnected')
@socketio.on('ntp_server')
def on_ntp_server():
recvTime = time.time()*1000
toSend = {
'recvTime': recvTime,
'sendTime': time.time()*1000
}
emit('ntp_client', toSend)
@socketio.on('update_room')
def on_update_room(data):
roomID = data['room']
if not roomExists(roomID):
createRoom(roomID)
updateRoomLightFunc(roomID, data['lightFuncData'])
toSend = {
'startTime': getRoomStartTime(roomID),
'frameCount': 0,
'lightFuncData': getRoomLightFunc(roomID)
}
socketio.emit('update', toSend, to=roomID)
emit('roomAvailable')
createRoom('123')
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0')