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.