feat: initial commit of AuraTrade quant terminal and sentinel risk engine
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import json
|
||||
import logging
|
||||
from typing import List, Dict, Any
|
||||
from fastapi import WebSocket
|
||||
|
||||
logger = logging.getLogger("AuraTrade.ConnectionManager")
|
||||
|
||||
class ConnectionManager:
|
||||
"""
|
||||
High-Frequency WebSocket Connection Hub
|
||||
Broadcasts sub-20ms ticks, telemetry, and signals to connected web clients.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.active_connections: List[WebSocket] = []
|
||||
|
||||
async def connect(self, websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
self.active_connections.append(websocket)
|
||||
logger.info(f"Client connected. Active terminals: {len(self.active_connections)}")
|
||||
|
||||
def disconnect(self, websocket: WebSocket):
|
||||
if websocket in self.active_connections:
|
||||
self.active_connections.remove(websocket)
|
||||
logger.info(f"Client disconnected. Active terminals: {len(self.active_connections)}")
|
||||
|
||||
async def broadcast_json(self, message: Dict[str, Any]):
|
||||
"""Broadcasts a JSON packet to all connected web terminals."""
|
||||
if not self.active_connections:
|
||||
return
|
||||
|
||||
payload = json.dumps(message)
|
||||
dead_connections = []
|
||||
for connection in self.active_connections:
|
||||
try:
|
||||
await connection.send_text(payload)
|
||||
except Exception:
|
||||
dead_connections.append(connection)
|
||||
|
||||
for dead in dead_connections:
|
||||
self.disconnect(dead)
|
||||
|
||||
ws_manager = ConnectionManager()
|
||||
@@ -0,0 +1,23 @@
|
||||
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
|
||||
from app.ws.connection_manager import ws_manager
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger("AuraTrade.WSRoutes")
|
||||
ws_router = APIRouter()
|
||||
|
||||
@ws_router.websocket("/ws/stream")
|
||||
async def websocket_endpoint(websocket: WebSocket):
|
||||
"""Unified WebSocket streaming endpoint for chart ticks, telemetry, and signals."""
|
||||
await ws_manager.connect(websocket)
|
||||
try:
|
||||
while True:
|
||||
# Keep connection alive & listen for client ping or incoming commands
|
||||
data = await websocket.receive_text()
|
||||
# If client sends ping, respond pong
|
||||
if data == "ping":
|
||||
await websocket.send_text("pong")
|
||||
except WebSocketDisconnect:
|
||||
ws_manager.disconnect(websocket)
|
||||
except Exception as e:
|
||||
logger.warning(f"WebSocket client error: {e}")
|
||||
ws_manager.disconnect(websocket)
|
||||
Reference in New Issue
Block a user