How to maintain the reconnection every 5 seconds after the connection to the server fails?

Gobal:maintain the reconnection after the connection to server failed or disconnect from server because of network fluctuation.
Action:I try like this code.
switch (ev)
case MG_EV_WS_OPEN:
{…}
break;
case MG_EV_WS_MSG:
{…}
break;
case MG_EV_CLOSE:
mg_connect(…)
break;
default:
break;
Result:It will reconnect to server only once.
Except:It won’t mg_call MG_EV_CLOSE after reconnect failed erver times like mongoose 6.xx because mg_connection.is_readable and mg_connection.is_writeable =0.And I check mg_connection.is_connecting then reconnect,But it will generate a lot of mg_connection in mgr.

Try this

int seconds = time(NULL);
while ( hdone == false) {
if( (time(NULL) - seconds) >= 5 ) {
seconds = time(NULL);
checkStuff();
}
mg_mgr_poll(&mgr, 1000); // Event loop
}