-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.py
More file actions
44 lines (34 loc) · 998 Bytes
/
router.py
File metadata and controls
44 lines (34 loc) · 998 Bytes
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
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
import asyncio
app = FastAPI()
host_ws = None
client_ws = None
pair_event = asyncio.Event()
async def pipe(server, client):
async def server_to_client():
async for data in server:
await client.send(data)
async def client_to_server():
async for data in client:
await server.send(data)
await asyncio.gather(server_to_client(), client_to_server())
@app.websocket("/host")
async def host_endpoint(ws: WebSocket):
global host_ws
await ws.accept()
host_ws = ws
if client_ws is not None:
pair_event.set()
try:
await asyncio.Future()
except asyncio.CancelledError:
pass
@app.websocket("/client")
async def client_endpoint(ws: WebSocket):
global client_ws
await ws.accept()
client_ws = ws
if host_ws is not None:
pair_event.set()
await pair_event.wait()
await pipe(host_ws, client_ws)