-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.py
121 lines (80 loc) · 2.35 KB
/
index.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
import datetime
from flask import Flask, request
from flask_socketio import SocketIO
from client import Client, BridgeClient, UserClient, ClientManager
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
io = SocketIO(app)
@app.route('/')
def index():
result = ''
for c in ClientManager.instance().all():
result += 'id: %s, type: %s\n' % (c.id, c.type)
return result
@io.on('connect')
def connect():
pass
@io.on('register-client')
def connect(data):
client_id = request.sid
client_type = data['type']
uuid = data['uuid']
check = ClientManager.instance().find(uuid)
if check is not None:
ClientManager.instance().delete(uuid)
if client_type == 'bridge':
BridgeClient(client_id, uuid)
elif client_type == 'user':
UserClient(client_id, uuid, data['target'])
else:
Client(client_id, client_type, uuid)
@io.on('ping')
def ping():
print("pong!", request.sid)
@io.on('disconnect')
def disconnect():
pass
@io.on('unregister-client')
def disconnect(data):
client_type = data['type']
client_uuid = data['uuid']
if client_type == 'bridge':
pass
ClientManager.instance().delete(client_uuid)
print("disconnect")
@io.on('message')
def message(data):
r = data['room']
text = data['text']
data['date'] = str(datetime.datetime.now())
client = ClientManager.instance().find(data['uuid'])
users = ClientManager.instance().find_users(client.uuid)
for user in users:
if r not in user.rooms:
json = {
'title': r,
'caption': text
}
user.rooms.append(r)
io.emit('room', json, room=user.id)
print('room', request.sid)
io.emit('message', data, room=user.id)
print('message', request.sid)
@io.on('room')
def room(data):
client = ClientManager.instance().find(data['uuid'])
for r in client.rooms:
io.emit('room', {
'title': r,
'caption': ''
}, room=client.id)
print('room', request.sid)
@io.on('send')
def send(data):
uuid = data['uuid']
client = ClientManager.instance().find(uuid)
target = ClientManager.instance().find(client.target_id)
io.emit('send', data, room=target.id)
print('send', request.sid)
if __name__ == '__main__':
io.run(app)