Spaces:
Runtime error
Runtime error
import websockets | |
import asyncio | |
import sys | |
URL = "wss://ramesh-vani-collab.hf.space" | |
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) 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() | |