I don’t generally like reviving old topics, but I’m trying to do exactly this and having no joy. Calling setsockopt on c->sock is returning EBADF (bad file descriptor).
struct mg_connection *c = mgos_mqtt_get_global_conn();
int on = 0;
sock_read = setsockopt(c->sock, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on));
LOG(LL_ERROR, ("Socket %d Keepalive set: %d", sock_read, errno));
Returns “Socket -1 Keepalive set: 9”
Where is the struct definition for mg_connection? It’s definitely different than the one in Cesanta’s net.h, which contains fd instead of sock.
Can we get a compile-time option to disable keepalive, and/or some sort of config option to set TCP_KEEPIDLE to something higher than 60 seconds? The default for this on linux is 2 HOURS, so having it fire 120x more than normal is very wasteful, especially on bandwidth-constrained environments like MTM cell networks. Given that there is already a keepalive in MQTT itself, I’d personally just prefer turning it off altogether to save the 114B per minute, which ends up eating through 160KB per day in useless chatter.