-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
42 lines (30 loc) · 827 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
41
42
from aiohttp import web
import socketio
import asyncio
app = web.Application()
sio = socketio.AsyncServer()
sio.attach(app)
connection_session = {
"aabid": None,
"umar": None,
}
@sio.event
async def connect(sid, environ, auth=None, headers=None):
print(environ)
if auth["to"] in connection_session and auth["from"] in connection_session:
connection_session[auth["from"]] = sid
await sio.save_session(sid, {"to": auth["to"]})
print(connection_session)
@sio.event
async def chat(sid, data):
print("message recieved", data)
to = await sio.get_session(sid)
print(to)
to = connection_session[to["to"]]
await sio.emit("chat", data, to=to)
return "oK"
@sio.event
def disconnect(sid):
print('disconnect ', sid)
if __name__ == '__main__':
web.run_app(app)