Spaces:
Build error
Build error
File size: 1,414 Bytes
9f26e80 77006af ea9f7e5 9f26e80 d0443bf 77006af 9f26e80 ea9f7e5 77006af ea9f7e5 77006af 9f26e80 77006af |
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 |
import asyncio
import websockets
import json
from fastapi import FastAPI
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI()
WEBSOCKET_URL = "ws://b917-160-179-178-105.ngrok-free.app/ws"
call_count = 0
async def websocket_client():
global call_count
while True:
try:
async with websockets.connect(WEBSOCKET_URL) as websocket:
logger.info("WebSocket connection established")
while True:
call_count += 1
payload = {"count": call_count}
await websocket.send(json.dumps(payload))
logger.info(f"Sent: {payload}")
response = await websocket.recv()
data = json.loads(response)
logger.info(f"Received: {data}")
await asyncio.sleep(1) # Adjust this delay as needed
except websockets.exceptions.ConnectionClosed:
logger.error("WebSocket connection closed. Retrying...")
await asyncio.sleep(5)
except Exception as e:
logger.error(f"Error: {e}")
await asyncio.sleep(5)
@app.on_event("startup")
async def startup_event():
asyncio.create_task(websocket_client())
@app.get("/")
async def root():
return {"message": "WebSocket client is running", "calls_made": call_count} |