From c4fc428f376d8e0e51865765d5d69d546ff4209c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 17 Dec 2017 21:27:55 +0100 Subject: [PATCH] Split initial WS message into submessages --- code/espurna/ws.ino | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 63becbb4..81770d28 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -300,10 +300,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { } -void _wsStart(uint32_t client_id) { - - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.createObject(); +void _wsOnStart(JsonObject& root) { bool changePassword = false; #if WEB_FORCE_PASS_CHANGE @@ -342,17 +339,14 @@ void _wsStart(uint32_t client_id) { root["tmpUnits"] = getSetting("tmpUnits", TMP_UNITS).toInt(); root["tmpCorrection"] = getSetting("tmpCorrection", TEMPERATURE_CORRECTION).toFloat(); - // Callbacks - for (unsigned char i = 0; i < _ws_on_send_callbacks.size(); i++) { - (_ws_on_send_callbacks[i])(root); - } - } - String output; - root.printTo(output); - wsSend(client_id, (char *) output.c_str()); +} +void _wsStart(uint32_t client_id) { + for (unsigned char i = 0; i < _ws_on_send_callbacks.size(); i++) { + wsSend(client_id, _ws_on_send_callbacks[i]); + } } void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ @@ -414,7 +408,7 @@ void wsSend(ws_on_send_callback_f callback) { callback(root); String output; root.printTo(output); - wsSend((char *) output.c_str()); + _ws.textAll((char *) output.c_str()); } } @@ -432,6 +426,15 @@ void wsSend_P(PGM_P payload) { } } +void wsSend(uint32_t client_id, ws_on_send_callback_f callback) { + DynamicJsonBuffer jsonBuffer; + JsonObject& root = jsonBuffer.createObject(); + callback(root); + String output; + root.printTo(output); + _ws.text(client_id, (char *) output.c_str()); +} + void wsSend(uint32_t client_id, const char * payload) { _ws.text(client_id, payload); } @@ -453,6 +456,7 @@ void wsSetup() { #if MQTT_SUPPORT mqttRegister(_wsMQTTCallback); #endif + wsOnSendRegister(_wsOnStart); wsOnAfterParseRegister(wsConfigure); }