danni / uwebsockets

Micropython websockets implementation
MIT License
182 stars 40 forks source link

Connection unstable - Websocket connection to server is dropped every 5s #18

Closed psykovski-extended closed 2 years ago

psykovski-extended commented 2 years ago

I've wrote a little program, where I continuously send data to my WebSocket Server:

async def publish_data():
    global data_busy, data
    while not iteration_done or len(data) < 2:
        await uasyncio.sleep(0.0001)
    temp = data
    data = []
    try:
        ws.send(str([token, temp]))
    except:
        connect_websocket()

The connection is not lost, because of not sending any data, it just happens every 5s or so. This code runs on an ESP32. Does anyone know, why this could happen?

carterw commented 2 years ago

That looks like a very tight loop. I expect you are running out of memory or something, the ESP32 is a very slow machine with only a small amount of memory. What happens when you use a delay of one or more seconds between sends?

psykovski-extended commented 2 years ago

Lack of memory can not be the error, because ESP32 would then raise an error. It actually worked a view weeks ago, the only thing i changed, was that there is a timer, which runs every 16ms and the ISR takes about 10ms to terminate. This is what the loop is for in publish_data(). The delay is regulating itself, because it waits til the data is sent. The thing that concerns me is that when the timer isr takes lesser time to terminate, it works perfectly, but how could those tasks interfere with each other?

psykovski-extended commented 2 years ago

Solves the issue:

The problem wasn't the client side, it was a server side problem. The server wanted to receive a keep-alive-ping ever 5s, If IT doesn't receive it, the connection gets dropped.