MCPcopy
hub / github.com/cft0808/edict / _relay_events

Function _relay_events

edict/backend/app/api/websocket.py:58–77  ·  view source on GitHub ↗

从 Redis Pub/Sub 接收事件,推送到 WebSocket。

(pubsub, ws: WebSocket)

Source from the content-addressed store, hash-verified

56
57
58async def _relay_events(pubsub, ws: WebSocket):
59 """从 Redis Pub/Sub 接收事件,推送到 WebSocket。"""
60 async for message in pubsub.listen():
61 if message["type"] == "pmessage":
62 channel = message["channel"]
63 data = message["data"]
64
65 # 提取 topic 名
66 topic = channel.replace("edict:pubsub:", "") if channel.startswith("edict:pubsub:") else channel
67
68 try:
69 event_data = json.loads(data) if isinstance(data, str) else data
70 await ws.send_json({
71 "type": "event",
72 "topic": topic,
73 "data": event_data,
74 })
75 except Exception as e:
76 log.warning(f"Failed to relay event: {e}")
77 break
78
79
80async def _handle_client_messages(ws: WebSocket):

Callers 1

websocket_endpointFunction · 0.85

Calls 1

send_jsonMethod · 0.80

Tested by

no test coverage detected