-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
108 lines (85 loc) · 3.54 KB
/
__init__.py
File metadata and controls
108 lines (85 loc) · 3.54 KB
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
import json
import time
import socketio
from fastapi import FastAPI
from configs import settings
from constants.ws import WS_MONITOR_EVENTS, Room, WSEvent
from server.helpers.redis_ import r
from server.utils.etc import get_server_ident
__all__ = [
"main",
"project",
"lesson",
"cursor",
"code",
"feedback",
"test",
]
def create_websocket(app: FastAPI, cors_allowed_origins: list | str):
message_queue = socketio.AsyncRedisManager(
f"{settings.REDIS_URL}/{settings.REDIS_DB}", redis_options=dict(socket_timeout=10, socket_connect_timeout=10)
)
kwargs = dict(
cors_allowed_origins=cors_allowed_origins,
async_mode="asgi",
client_manager=message_queue,
logger=settings.WS_DEBUG,
engineio_logger=settings.WS_DEBUG,
)
sio = CompatibleAsyncServer(**kwargs) if not settings.WS_MONITOR else AsyncServerForMonitor(**kwargs)
return sio, socketio.ASGIApp(sio, app)
class CompatibleAsyncServer(socketio.AsyncServer):
"""For compatibility with AsyncServerForMonitor"""
async def emit(
self, event, data=None, to=None, room=None, skip_sid=None, namespace=None, callback=None, uuid=None, **kwargs
):
"""Remove ``uuid`` from kwargs"""
return await super().emit(event, data, to, room, skip_sid, namespace, callback, **kwargs)
message_box = {}
class AsyncServerForMonitor(socketio.AsyncServer):
@property
def _timestamp(self):
return int(time.time() * 1000)
async def emit(
self, event, data=None, to=None, room=None, skip_sid=None, namespace=None, callback=None, uuid=None, **kwargs
):
"""Inject data for debug
Args:
uuid (str | None): UUID of invoking this emit
"""
if uuid and type(data) == dict:
data["uuid"] = uuid
d = message_box.pop(uuid, None)
if d:
data.update(d)
return await super().emit(event, data, to, room, skip_sid, namespace, callback, **kwargs)
async def _emit_internal(self, sid, event, data, namespace=None, id=None):
if type(data) == dict and event not in WS_MONITOR_EVENTS:
data["_ts_3"] = self._timestamp
data["_ts_3_eid"] = sid # event to. (sid == eio_sid) in this method
data["_s_emit"] = event
return await super()._emit_internal(sid, event, data, namespace, id)
async def _handle_event_internal(self, server, sid, eio_sid, data, namespace, id):
from server.websockets import session as ws_session
try:
# if event != TIMESTAMP_ACK, then inject data
if data[0] not in WS_MONITOR_EVENTS and type(data[1]) == dict and "uuid" in data[1]:
message_box[data[1]["uuid"]] = {
"_ts_1": data[1].get("_ts_1"),
"_ts_1_eid": eio_sid, # event by
"_ts_2": self._timestamp,
"_c_emit": data[0],
}
data[1]["server"] = get_server_ident()
course_id = await ws_session.get(sid, "course_id")
lesson_id = await ws_session.get(sid, "lesson_id")
monitor_room = Room.WS_MONITOR.format(course_id=course_id, lesson_id=lesson_id)
await self.emit(
WSEvent.WS_MONITOR_EVENT,
data=data[1],
room=monitor_room,
uuid=data[1]["uuid"],
)
except:
pass
return await super()._handle_event_internal(server, sid, eio_sid, data, namespace, id)