@@ -242,22 +242,26 @@ class BaseCloudStorage {
242242 // when _connectionState is either NOT_CONNECTED on FAILED
243243 // After succesfully connecting, it will send all pending keys to the server (will start listening on them)
244244 void startListeningForUpdates () {
245- if (this ->_connectionState != WS_STATE_NOT_CONNECTED && this ->_connectionState != WS_STATE_FAILED) {
245+ if (this ->_connectionState != WS_STATE_NOT_CONNECTED &&
246+ this ->_connectionState != WS_STATE_FAILED) {
246247 // See constrains.
247248 return ;
248249 }
249250
250251 this ->_connectionState = WS_STATE_CONNECTING;
251- client.onMessage ([&](websockets::WebsocketsMessage msg) {
252- String data (msg.data ().c_str ());
252+ client.onMessage ([this ](websockets::WebsocketsMessage msg) {
253+ // String data(msg.data().c_str());
254+ // Serial.println("Got Msg: " + data);
253255
254256 // parse response json
255- StaticJsonBuffer<300 > jsonBuffer;
256- JsonObject& root = jsonBuffer.parseObject (data);
257+ StaticJsonBuffer<500 > jsonBuffer;
258+ JsonObject& root = jsonBuffer.parseObject (msg.data ());
259+
260+ String type = root[" type" ];
261+ bool isError = root[" error" ];
257262
258263 // If the message is about the login, proccess it and update state
259- if (root[" type" ] == " login" ) {
260- bool isError = root[" error" ];
264+ if (type == " login" ) {
261265 if (isError) {
262266 // in case of bad login, set to fail and close connection
263267 this ->_connectionState = WS_STATE_FAILED;
@@ -270,19 +274,18 @@ class BaseCloudStorage {
270274 }
271275 this ->_keysPendingConnection .clear ();
272276 }
277+ return ;
273278 }
274-
275- if (root[" error" ]) return ;
276- String type = root[" type" ];
277-
279+
280+ if (isError) return ;
278281 // if the message is about a changed key, let the user know
279282 if (type == " value-changed" ) {
280283 String key = root[" result" ][" key" ];
281284 this ->_listenCallback (key);
282285 }
283286 });
284287 auto connectString = this ->_baseServerUrl + " /listen" ;
285- bool didConnect = client.connect (connectString. c_str () );
288+ bool didConnect = client.connect (connectString);
286289 // check if connection was successfull
287290 if (didConnect) {
288291 // send login string
0 commit comments