Skip to content

Commit 6e92f4c

Browse files
authored
Merge pull request #8 from gilmaimon/feature-realtime-updates
Feature realtime updates
2 parents c85710c + 61b3df4 commit 6e92f4c

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoCloudStorage
2-
version=0.6.0
2+
version=0.6.1
33
author=Gil Maimon <mail.gilmaimon@gmail.com>
44
maintainer=Gil Maimon <mail.gilmaimon@gmail.com>
55
sentence=CloudStorage lets you store and retrive values from a remote server.

src/CloudStorage.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/Http/GenericEspRequestImpl.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ namespace http {
3333
Response response;
3434
response.statusCode = httpCode;
3535

36-
if(httpCode == 200) response.body = this->_http.getString();
36+
if(httpCode == 200) {
37+
response.body = "";//this->_http.getString();
38+
while(_client.available()) {
39+
char ch = _client.read();
40+
response.body += ch;
41+
}
42+
}
3743
else response.body = this->_http.errorToString(httpCode);
38-
39-
this->_http.end();
4044

4145
return response;
4246
}

0 commit comments

Comments
 (0)