0

So I'm transfering from node into FastAPI (Because it's more convinient for me) And I have a webpage (full code here), which was working in node, but when transferred to FastAPI only starting websocket messages get through, the one in loop get lost (though problem is only with this particular page, other pages in project receives them). Here's FastAPI code:

@app.websocket("/websocket")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    await websocket.send_json({"event": "infobar", "content": info_bar})
    await websocket.send_json({"event": "show_emblem", "value": emblem_visible})
    await websocket.send_json({"event": "predefs", "content": predefs})
    await websocket.send_json({"event": "candidates", "content": candidates})
    await websocket.send_json({"event": "timer_state", "state": timer.__dict__()})
    while True:
        data = await websocket.receive_json()
        print(data)
        if data["event"] == "timer":
            if data["type"] == "start" and not timer.running:
                timer.running = True
                timer.started_at = datetime.datetime.now()
                await websocket.send_json({"event": "timer_state", "state": timer.__dict__()})
            elif data["type"] == "stop":
                UpdateTimer()
                timer.running = False
                await websocket.send_json({"event": "timer_state", "state": timer.__dict__()})

            elif["type"] == "set":
                timer.running = False
                timer.time = int(data["time"])
                await websocket.send_json({"event": "timer_state", "state": timer.__dict__()})
        else:
            print(data)
            await websocket.send_json({"event": "Test_event", "content": "Missing"})
            print("Dispatching")
Moder New
  • 457
  • 1
  • 5
  • 18
  • To make sure - you receive data but `send_json` sends nothing? Does it hang somewhere? Can you receive more data? – kosciej16 Jan 09 '22 at 20:00
  • 1
    @kosciej16 managed to fix it, had to write simple connection manager will add answer to question tommorow, I'm too tired today – Moder New Jan 09 '22 at 22:45
  • @ModerNew I'd be really curious about the final code since I'm facing a similar issue. Would you please share it? – MasovyKnedlicek Jul 26 '22 at 13:24

0 Answers0