Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Why does the heartbeat continue after disconnection ? #812

Unanswered
M0wiki asked this question in Q&A
Discussion options

What am I trying to do ?

I am trying to build a simple connection mechanism based on the SocketIO protocol :

  • A NodeMCU device (ESP8266) running the arduinoWebSockets library connects to a Flask server running the Flask-SocketIO library, providing its ID (a 24-character alphanumeric string)
  • If the NodeMCU's ID is part of the "authorized" IDs, the SocketIO connection sould be established normally
  • But if If the NodeMCU's ID is not part of the "authorized" IDs, the server should disconnect the NodeMCU and thus close the SocketIO connection

The problem :

In both cases, and despite the call to the disconnect() function in the second case, the SocketIO connection remains established. Indeed, the heartbeat mechanism triggers, and the server and the client start exchanging PINGs and PONGs at regular time intervals.

Some logs to help further understand the problem :

  • When the ID is authorized (nothing wrong apparently)

· Server logs

(27040) wsgi starting up on http://0.0.0.0:8080
(27040) accepted ('192.168.43.40', 53686)
[2023年04月05日 16:10:57,764] qZZepzzc1pmo7PQDAAAA: Sending packet OPEN data {'sid': 'qZZepzzc1pmo7PQDAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000}
192.168.43.40 - - [05/Apr/2023 16:10:57] "GET /socket.io/?EIO=4&id=G07y2eO2HP9oy3TwvDA3oH3k&transport=polling HTTP/1.1" 200 316 0.003003
[2023年04月05日 16:10:57,787] qZZepzzc1pmo7PQDAAAA: Received request to upgrade to websocket
[2023年04月05日 16:10:57,805] qZZepzzc1pmo7PQDAAAA: Upgrade to websocket successful
[2023年04月05日 16:10:57,807] qZZepzzc1pmo7PQDAAAA: Received packet MESSAGE data 0/
NodeMCU trying to connect with ID : G07y2eO2HP9oy3TwvDA3oH3k
Connection allowed.
[2023年04月05日 16:10:57,808] qZZepzzc1pmo7PQDAAAA: Sending packet MESSAGE data 0{"sid":"Vu79-VDhIjVIu6cnAAAB"}
[2023年04月05日 16:11:22,768] qZZepzzc1pmo7PQDAAAA: Sending packet PING data None
[2023年04月05日 16:11:22,883] qZZepzzc1pmo7PQDAAAA: Received packet PONG data
[2023年04月05日 16:11:47,883] qZZepzzc1pmo7PQDAAAA: Sending packet PING data None
[2023年04月05日 16:11:47,972] qZZepzzc1pmo7PQDAAAA: Received packet PONG data
[2023年04月05日 16:12:12,994] qZZepzzc1pmo7PQDAAAA: Sending packet PING data None
[2023年04月05日 16:12:13,057] qZZepzzc1pmo7PQDAAAA: Received packet PONG data

· NodeMCU logs

[SETUP] WiFi Connected 192.168.43.40
[IOc] Connected to url: /socket.io/?EIO=4&id=G07y2eO2HP9oy3TwvDA3oH3k
pm open,type:2 0
  • When the ID is not authorized (something wrong)

· Server logs

(4276) wsgi starting up on http://0.0.0.0:8080
(4276) accepted ('192.168.43.40', 52604)
[2023年04月05日 16:06:38,656] iTfJ7NmwAsDUC0mPAAAA: Sending packet OPEN data {'sid': 'iTfJ7NmwAsDUC0mPAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000}
192.168.43.40 - - [05/Apr/2023 16:06:38] "GET /socket.io/?EIO=4&id=Jo6282QNd2Y9e3uA6AUpbjmZ&transport=polling HTTP/1.1" 200 316 0.000995
[2023年04月05日 16:06:38,690] iTfJ7NmwAsDUC0mPAAAA: Received request to upgrade to websocket
[2023年04月05日 16:06:38,730] iTfJ7NmwAsDUC0mPAAAA: Upgrade to websocket successful
[2023年04月05日 16:06:38,734] iTfJ7NmwAsDUC0mPAAAA: Received packet MESSAGE data 0/
NodeMCU trying to connect with ID : Jo6282QNd2Y9e3uA6AUpbjmZ
Connection rejected.
[2023年04月05日 16:06:38,735] iTfJ7NmwAsDUC0mPAAAA: Sending packet MESSAGE data 1
Client disconnected OyPQi0eLbAob7lW8AAAB
[2023年04月05日 16:06:38,735] iTfJ7NmwAsDUC0mPAAAA: Sending packet MESSAGE data 0{"sid":"OyPQi0eLbAob7lW8AAAB"}
[2023年04月05日 16:07:03,656] iTfJ7NmwAsDUC0mPAAAA: Sending packet PING data None
[2023年04月05日 16:07:03,810] iTfJ7NmwAsDUC0mPAAAA: Received packet PONG data
[2023年04月05日 16:07:28,806] iTfJ7NmwAsDUC0mPAAAA: Sending packet PING data None
[2023年04月05日 16:07:28,898] iTfJ7NmwAsDUC0mPAAAA: Received packet PONG data
[2023年04月05日 16:07:53,912] iTfJ7NmwAsDUC0mPAAAA: Sending packet PING data None
[2023年04月05日 16:07:53,988] iTfJ7NmwAsDUC0mPAAAA: Received packet PONG data

· NodeMCU logs

[SETUP] WiFi Connected 192.168.43.40
[IOc] Connected to url: /socket.io/?EIO=4&id=Jo6282QNd2Y9e3uA6AUpbjmZ
[IOc] Disconnected!
pm open,type:2 0

The minimal tested code:

Here is the minimal Flask webapp code :

from flask import Flask, request
from flask_socketio import SocketIO, disconnect
socketio = SocketIO()
app = Flask(__name__)
socketio.init_app(app, cors_allowed_origins='*', engineio_logger=True, async_mode="eventlet")
IDS_ALLOWED_TO_CONNECT = {"G07y2eO2HP9oy3TwvDA3oH3k", "Y60zX1a3Zdt6OIBSC2mb3pp4"}
@app.route('/')
def entry_point():
 return 'Hello World!'
## SOCKET-IO LOGIC
@socketio.on('connect')
def on_connect():
 id = request.args.get('id')
 if id is not None:
 print("NodeMCU trying to connect with ID :",id)
 if id in IDS_ALLOWED_TO_CONNECT:
 print("Connection allowed.")
 else:
 print("Connection rejected.")
 disconnect()
 else:
 print("NodeMCU trying to connect without an ID : Connection rejected.")
 disconnect()
@socketio.on('test_event')
def on_test_event(message):
 print("Message from client :", message)
@socketio.on('disconnect')
def on_disconnect():
 print('Client disconnected', request.sid)
if __name__ == '__main__':
 socketio.run(app, host='0.0.0.0', port=8080, debug=True)

and here is the minimal NodeMCU code :

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ArduinoJson.h>
#include <WebSocketsClient.h>
#include <SocketIOclient.h>
#include <Hash.h>
ESP8266WiFiMulti WiFiMulti;
SocketIOclient socketIO;
#define USE_SERIAL Serial
void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
 switch(type) {
 case sIOtype_DISCONNECT:
 USE_SERIAL.printf("[IOc] Disconnected!\n");
 break;
 case sIOtype_CONNECT:
 USE_SERIAL.printf("[IOc] Connected to url: %s\n", payload);
 // join default namespace (no auto join in Socket.IO V3)
 socketIO.send(sIOtype_CONNECT, "/");
 break;
 case sIOtype_EVENT:
 USE_SERIAL.printf("[IOc] get event: %s\n", payload);
 break;
 case sIOtype_ACK:
 USE_SERIAL.printf("[IOc] get ack: %u\n", length);
 hexdump(payload, length);
 break;
 case sIOtype_ERROR:
 USE_SERIAL.printf("[IOc] get error: %u\n", length);
 hexdump(payload, length);
 break;
 case sIOtype_BINARY_EVENT:
 USE_SERIAL.printf("[IOc] get binary: %u\n", length);
 hexdump(payload, length);
 break;
 case sIOtype_BINARY_ACK:
 USE_SERIAL.printf("[IOc] get binary ack: %u\n", length);
 hexdump(payload, length);
 break;
 }
}
void setup() {
 USE_SERIAL.begin(115200);
 USE_SERIAL.setDebugOutput(true);
 USE_SERIAL.println();
 USE_SERIAL.println();
 USE_SERIAL.println();
 for(uint8_t t = 4; t > 0; t--) {
 USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
 USE_SERIAL.flush();
 delay(1000);
 }
 // disable AP
 if(WiFi.getMode() & WIFI_AP) {
 WiFi.softAPdisconnect(true);
 }
 WiFiMulti.addAP("SSID", "MDP");
 //WiFi.disconnect();
 while(WiFiMulti.run() != WL_CONNECTED) {
 delay(100);
 }
 String ip = WiFi.localIP().toString();
 USE_SERIAL.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
 // server address, port and URL
 socketIO.begin("SERVER_ADDR", 8080, "/socket.io/?EIO=4&id=G07y2eO2HP9oy3TwvDA3oH3k");
 // event handler
 socketIO.onEvent(socketIOEvent);
}
void loop() {
	
 socketIO.loop();
	
}

Thank you very much for your help !

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /