File size: 2,506 Bytes
ab77fd3
 
7a24084
ab77fd3
7a24084
 
ab77fd3
7a24084
 
ab77fd3
7a24084
ab77fd3
7a24084
ab77fd3
 
 
 
 
39900cc
ab77fd3
 
 
 
308313f
ab77fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4ae883
 
7a24084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308313f
7a24084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308313f
e4ae883
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
# main.py (Simplified for Pure WebSockets)
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
# πŸ›‘ REMOVE: from aiortc import RTCPeerConnection, RTCSessionDescription, ...
import json
import re
# ... (rest of imports)

app = FastAPI()
# ... (CORS setup remains) ...

# πŸ›‘ REMOVE: ice_servers and rtc_config

# πŸš€ NEW: Pure WebSocket endpoint for RPC
@app.websocket("/ws") 
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    print("βœ… WebSocket connected for RPC")

    try:
        while True:
            # Receive RPC message from the client
            message = await websocket.receive_text()
            print("πŸ“© Received:", message)
            
            try:
                rpc = json.loads(message)
                result = handle_rpc(rpc)

                response = json.dumps({
                    "jsonrpc": "2.0",
                    "id": rpc["id"],
                    "result": result,
                })
                
                # Send the RPC result back over the WebSocket
                await websocket.send_text(response)

            except json.JSONDecodeError:
                print(f"Error decoding RPC message: {message}")

    except WebSocketDisconnect:
        print("πŸ”Œ WebSocket disconnected.")
    # No RTCPeerConnection cleanup needed!

# βœ… Your RPC logic stays the same
def handle_rpc(rpc):
    method = rpc.get("method")
    params = rpc.get("params", {})

    if method == "echo":
        return params.get("message", "")

    elif method == "process":
        message = params.get("message", "").lower()

        # Add
        match = re.match(r"add (\d+) and (\d+)", message)
        if match:
            x, y = map(int, match.groups())
            return x + y

        # Subtract
        match = re.match(r"subtract (\d+) from (\d+)", message)
        if match:
            y, x = map(int, match.groups())
            return x - y

        # Multiply
        match = re.match(r"multiply (\d+) and (\d+)", message)
        if match:
            x, y = map(int, match.groups())
            return x * y

        # Divide
        match = re.match(r"divide (\d+) by (\d+)", message)
        if match:
            x, y = map(int, match.groups())
            return round(x / y, 2) if y != 0 else "Division by zero"

        return "Unknown command"

    else:
        return "Unknown method"


print("βœ… FastAPI WebRTC backend with TURN is ready")