Hanzo03 commited on
Commit
ab77fd3
Β·
1 Parent(s): e4ae883
Files changed (1) hide show
  1. main.py +35 -73
main.py CHANGED
@@ -1,85 +1,47 @@
1
- # main.py
2
- from fastapi import FastAPI, Request
3
  from fastapi.middleware.cors import CORSMiddleware
4
- from aiortc import RTCPeerConnection, RTCSessionDescription, RTCConfiguration, RTCIceServer
5
  import json
6
  import re
 
7
 
8
  app = FastAPI()
 
9
 
10
- # Enable CORS
11
- app.add_middleware(
12
- CORSMiddleware,
13
- allow_origins=["*"],
14
- allow_methods=["*"],
15
- allow_headers=["*"],
16
- )
17
 
18
- peer_connections = {}
 
 
 
 
19
 
20
- # βœ… Add STUN + TURN servers here
21
- ice_servers = [
22
- RTCIceServer(urls="stun:stun.l.google.com:19302"),
23
- RTCIceServer(
24
- urls="turn:relay1.expressturn.com:3478",
25
- username="efl-projects",
26
- credential="turn123"
27
- )
28
- ]
29
-
30
- rtc_config = RTCConfiguration(iceServers=ice_servers)
31
-
32
-
33
- @app.post("/signal")
34
- async def signal(request: Request):
35
- body = await request.json()
36
- offer = body["offer"]
37
-
38
- # βœ… Peer connection with TURN support
39
- pc = RTCPeerConnection(rtc_config)
40
- peer_connections[id(pc)] = pc # prevent GC
41
-
42
- @pc.on("datachannel")
43
- def on_datachannel(channel):
44
- print("βœ… Data channel opened")
45
-
46
- @channel.on("message")
47
- def on_message(message):
48
  print("πŸ“© Received:", message)
49
-
50
- rpc = json.loads(message)
51
- result = handle_rpc(rpc)
52
-
53
- response = json.dumps({
54
- "jsonrpc": "2.0",
55
- "id": rpc["id"],
56
- "result": result,
57
- })
58
-
59
- print("πŸ“€ Sending:", response)
60
- channel.send(response)
61
-
62
- # Apply remote SDP
63
- await pc.setRemoteDescription(
64
- RTCSessionDescription(
65
- sdp=offer["sdp"],
66
- type=offer["type"]
67
- )
68
- )
69
-
70
- # Create & return answer
71
- answer = await pc.createAnswer()
72
- await pc.setLocalDescription(answer)
73
-
74
- print("βœ… Answer generated and sent")
75
-
76
- return {
77
- "answer": {
78
- "sdp": pc.localDescription.sdp,
79
- "type": pc.localDescription.type
80
- }
81
- }
82
-
83
 
84
  # βœ… Your RPC logic stays the same
85
  def handle_rpc(rpc):
 
1
+ # main.py (Simplified for Pure WebSockets)
2
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
3
  from fastapi.middleware.cors import CORSMiddleware
4
+ # πŸ›‘ REMOVE: from aiortc import RTCPeerConnection, RTCSessionDescription, ...
5
  import json
6
  import re
7
+ # ... (rest of imports)
8
 
9
  app = FastAPI()
10
+ # ... (CORS setup remains) ...
11
 
12
+ # πŸ›‘ REMOVE: ice_servers and rtc_config
 
 
 
 
 
 
13
 
14
+ # πŸš€ NEW: Pure WebSocket endpoint for RPC
15
+ @app.websocket("/ws")
16
+ async def websocket_endpoint(websocket: WebSocket):
17
+ await websocket.accept()
18
+ print("βœ… WebSocket connected for RPC")
19
 
20
+ try:
21
+ while True:
22
+ # Receive RPC message from the client
23
+ message = await websocket.receive_text()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  print("πŸ“© Received:", message)
25
+
26
+ try:
27
+ rpc = json.loads(message)
28
+ result = handle_rpc(rpc)
29
+
30
+ response = json.dumps({
31
+ "jsonrpc": "2.0",
32
+ "id": rpc["id"],
33
+ "result": result,
34
+ })
35
+
36
+ # Send the RPC result back over the WebSocket
37
+ await websocket.send_text(response)
38
+
39
+ except json.JSONDecodeError:
40
+ print(f"Error decoding RPC message: {message}")
41
+
42
+ except WebSocketDisconnect:
43
+ print("πŸ”Œ WebSocket disconnected.")
44
+ # No RTCPeerConnection cleanup needed!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  # βœ… Your RPC logic stays the same
47
  def handle_rpc(rpc):