init
Browse files
main.py
CHANGED
|
@@ -1,85 +1,47 @@
|
|
| 1 |
-
# main.py
|
| 2 |
-
from fastapi import FastAPI,
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from aiortc import RTCPeerConnection, RTCSessionDescription,
|
| 5 |
import json
|
| 6 |
import re
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
app.add_middleware(
|
| 12 |
-
CORSMiddleware,
|
| 13 |
-
allow_origins=["*"],
|
| 14 |
-
allow_methods=["*"],
|
| 15 |
-
allow_headers=["*"],
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 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 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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):
|