Skip to content

Commit

Permalink
Merge pull request #8 from gilmaimon/feature-realtime-updates
Browse files Browse the repository at this point in the history
Feature realtime updates
  • Loading branch information
gilmaimon authored Mar 15, 2019
2 parents c85710c + 61b3df4 commit 6e92f4c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ArduinoCloudStorage
version=0.6.0
version=0.6.1
author=Gil Maimon <[email protected]>
maintainer=Gil Maimon <[email protected]>
sentence=CloudStorage lets you store and retrive values from a remote server.
Expand Down
27 changes: 15 additions & 12 deletions src/CloudStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,22 +242,26 @@ class BaseCloudStorage {
// when _connectionState is either NOT_CONNECTED on FAILED
// After succesfully connecting, it will send all pending keys to the server (will start listening on them)
void startListeningForUpdates() {
if(this->_connectionState != WS_STATE_NOT_CONNECTED && this->_connectionState != WS_STATE_FAILED) {
if(this->_connectionState != WS_STATE_NOT_CONNECTED &&
this->_connectionState != WS_STATE_FAILED) {
// See constrains.
return;
}

this->_connectionState = WS_STATE_CONNECTING;
client.onMessage([&](websockets::WebsocketsMessage msg) {
String data(msg.data().c_str());
client.onMessage([this](websockets::WebsocketsMessage msg) {
//String data(msg.data().c_str());
//Serial.println("Got Msg: " + data);

// parse response json
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(data);
StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(msg.data());

String type = root["type"];
bool isError = root["error"];

// If the message is about the login, proccess it and update state
if(root["type"] == "login") {
bool isError = root["error"];
if(type == "login") {
if(isError) {
// in case of bad login, set to fail and close connection
this->_connectionState = WS_STATE_FAILED;
Expand All @@ -270,19 +274,18 @@ class BaseCloudStorage {
}
this->_keysPendingConnection.clear();
}
return;
}

if(root["error"]) return;
String type = root["type"];


if(isError) return;
// if the message is about a changed key, let the user know
if(type == "value-changed") {
String key = root["result"]["key"];
this->_listenCallback(key);
}
});
auto connectString = this->_baseServerUrl + "/listen";
bool didConnect = client.connect(connectString.c_str());
bool didConnect = client.connect(connectString);
// check if connection was successfull
if(didConnect) {
// send login string
Expand Down
10 changes: 7 additions & 3 deletions src/Http/GenericEspRequestImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ namespace http {
Response response;
response.statusCode = httpCode;

if(httpCode == 200) response.body = this->_http.getString();
if(httpCode == 200) {
response.body = "";//this->_http.getString();
while(_client.available()) {
char ch = _client.read();
response.body += ch;
}
}
else response.body = this->_http.errorToString(httpCode);

this->_http.end();

return response;
}
Expand Down

0 comments on commit 6e92f4c

Please sign in to comment.