So I was investigating more on this: https://github.com/vshymanskyy/blynk-library-js/issues/40
And what is happening in my case is that sometimes it enters a loop where the tcp socket receives a msg_id == 0, so it disconnects and tries to reconnect and receive again a msg_id == 0 and so on in a loop. If using SSL it leads to crash after a while.
This is the code in blynk.js (line 397) that makes the socket disconnect:
if (msg_id === 0) { return self.disconnect(); }
Any idea on what the msg id == 0 is (BLYNK_CMD_RESPONSE), why it is received after connection and why the library disconnects when receiving it?
Can I safely remove that disconnect in the JS library?
I can’t the full code (it’s 2 scripts actually and 9000+ lines and there is some proprietary code). I can try to extract the blynk related code, is it ok?
if (conn.connected()) {
while (avail || conn.available() > 0) {
//BLYNK_LOG2(BLYNK_F("Available: "), conn.available());
//const unsigned long t = micros();
if (!processInput()) {
conn.disconnect();
// TODO: Only when in direct mode?
#ifdef BLYNK_USE_DIRECT_CONNECT
state = CONNECTING;
#endif
BlynkOnDisconnected();
return false;
}
avail = false;
//BLYNK_LOG2(BLYNK_F("Proc time: "), micros() - t);
}
}
From what we see from the C ++ library the msg_id == 0 is equivalent to having received a “bad header” and, if connected, forces the disconnection.
The interesting question is why do you get a message with id 0?
Do you use the blynk cloud or local server?
Could you try to log the messages of the protocol that you receive and send ??