-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
70 lines (55 loc) · 1.49 KB
/
main.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
from flask import *
from flask_socketio import *
from threading import Lock
thread = None
thread_lock = Lock()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
users = {}
@app.route("/")
def homepage():
return render_template("homepage.html")
@app.route("/skrible")
def skrible():
return render_template("skrible.html")
@socketio.on("draw")
def recvmsg(JSON):
emit('click', JSON, broadcast = True)
uid_max = 0
@socketio.on("newuser")
def newuser(username):
global users, uid_max
uid_max += 1
users[uid_max] = username
emit("onlineusers", users, broadcast=True)
print("debug:",users)
return uid_max
@socketio.on("msg")
def chatting(msg):
emit('chatting', msg, broadcast = True)
@socketio.on("disconnecting")
def disc(userid):
users.pop(userid)
emit("onlineusers", users, broadcast=True)
def backgroundThread(users):
activeuser = 0
while True:
if (len(users) == 0):
socketio.sleep(10)
else:
dic = dict(users)
print(dic)
for uid in dic:
print(uid)
socketio.emit("activeuser", uid, broadcast=True)
socketio.sleep(20)
@socketio.on('connect')
def connect():
global thread
global users
with thread_lock:
if thread is None:
thread = socketio.start_background_task(backgroundThread, users)
if __name__ == '__main__':
socketio.run(app, host="0.0.0.0", debug=True)