|
| 1 | +import asyncio |
| 2 | +import redis.asyncio as redis |
| 3 | +import websockets |
| 4 | +import os |
| 5 | +import signal |
| 6 | + |
| 7 | +# Config |
| 8 | +REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0") |
| 9 | +REDIS_CHANNEL = "can_messages" |
| 10 | +WS_PORT = int(os.getenv("WS_PORT", 9080)) |
| 11 | + |
| 12 | +connected_clients = set() |
| 13 | +shutdown_event = asyncio.Event() |
| 14 | + |
| 15 | +async def redis_listener(): |
| 16 | + """Listens to Redis and broadcasts to all WS clients.""" |
| 17 | + try: |
| 18 | + r = redis.from_url(REDIS_URL) |
| 19 | + pubsub = r.pubsub() |
| 20 | + await pubsub.subscribe(REDIS_CHANNEL) |
| 21 | + print(f"[*] Subscribed to Redis channel: {REDIS_CHANNEL}") |
| 22 | + |
| 23 | + async for message in pubsub.listen(): |
| 24 | + if shutdown_event.is_set(): |
| 25 | + break |
| 26 | + |
| 27 | + if message['type'] == 'message': |
| 28 | + data = message['data'] |
| 29 | + if isinstance(data, bytes): |
| 30 | + data = data.decode('utf-8') |
| 31 | + |
| 32 | + # Broadcast to all connected clients |
| 33 | + if connected_clients: |
| 34 | + # Create tasks for sending to each client to avoid blocking |
| 35 | + await asyncio.gather( |
| 36 | + *[client.send(data) for client in connected_clients], |
| 37 | + return_exceptions=True |
| 38 | + ) |
| 39 | + except Exception as e: |
| 40 | + print(f"[!] Redis error: {e}") |
| 41 | + finally: |
| 42 | + print("[*] Redis listener stopping...") |
| 43 | + |
| 44 | +async def ws_handler(websocket): |
| 45 | + """Manages WebSocket connections.""" |
| 46 | + connected_clients.add(websocket) |
| 47 | + client_info = f"{websocket.remote_address[0]}:{websocket.remote_address[1]}" |
| 48 | + print(f"[+] Client connected: {client_info}. Total: {len(connected_clients)}") |
| 49 | + |
| 50 | + try: |
| 51 | + await websocket.wait_closed() |
| 52 | + except Exception as e: |
| 53 | + print(f"[!] Error while waiting for websocket {client_info} to close: {e}") |
| 54 | + finally: |
| 55 | + connected_clients.remove(websocket) |
| 56 | + print(f"[-] Client disconnected: {client_info}. Total: {len(connected_clients)}") |
| 57 | + |
| 58 | +async def main(): |
| 59 | + loop = asyncio.get_running_loop() |
| 60 | + |
| 61 | + # Handle graceful shutdown |
| 62 | + def handle_signal(): |
| 63 | + print("\n[*] Shutting down...") |
| 64 | + shutdown_event.set() |
| 65 | + |
| 66 | + for sig in (signal.SIGINT, signal.SIGTERM): |
| 67 | + loop.add_signal_handler(sig, handle_signal) |
| 68 | + |
| 69 | + print(f"[*] Starting WebSocket Bridge on port {WS_PORT}...") |
| 70 | + |
| 71 | + # Start WebSocket server |
| 72 | + async with websockets.serve(ws_handler, "0.0.0.0", WS_PORT): |
| 73 | + print(f"[*] WebSocket server running at ws://0.0.0.0:{WS_PORT}") |
| 74 | + |
| 75 | + # Run Redis listener until shutdown |
| 76 | + await redis_listener() |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + try: |
| 80 | + asyncio.run(main()) |
| 81 | + except KeyboardInterrupt: |
| 82 | + print("\n[*] Keyboard interrupt received, exiting...") |
0 commit comments