Spaces:
Paused
Paused
| import websockets | |
| import asyncio | |
| import sys | |
| import os | |
| URL = "wss://ramesh-vani-collabtest-bk5.hf.space" | |
| headers = {'Authorization': f'Bearer {os.getenv("auth")}'} | |
| async def receiver(ws,connection): | |
| try: | |
| async for msg in connection: | |
| # msg = await connection.recv() | |
| print(f"\nServer: {msg}") | |
| await ws.send(msg) | |
| except websockets.exceptions.WebSocketException as e: | |
| print(f"WebSocket exception: {e}") | |
| # sys.exit(1) | |
| async def sender(ws,connection): | |
| try: | |
| async for msg in ws: | |
| # msg = await ws.recv() | |
| await connection.send(msg) | |
| except websockets.exceptions.WebSocketException as e: | |
| print(f"WebSocket exception: {e}") | |
| # sys.exit(1) | |
| async def chat(websocket, path) -> None: | |
| try: | |
| async with websockets.connect(URL,extra_headers=headers) as ws: | |
| await asyncio.gather( | |
| receiver(websocket,ws), | |
| sender(websocket,ws) | |
| ) | |
| except websockets.exceptions.WebSocketException as e: | |
| #print(f"WebSocket exception: {e}") | |
| sys.exit(1) | |
| start_server = websockets.serve(chat,"0.0.0.0", 7860) | |
| asyncio.get_event_loop().run_until_complete(start_server) | |
| asyncio.get_event_loop().run_forever() | |