diff --git a/Firebase.cpp b/Firebase.cpp index 69cd0150..6752811a 100644 --- a/Firebase.cpp +++ b/Firebase.cpp @@ -55,44 +55,41 @@ FirebaseRemove Firebase::remove(const String& path) { } FirebaseStream Firebase::stream(const String& path) { - return FirebaseStream(host_, auth_, path); // stream doesn't reuse http client. + // TODO: create new client dedicated to stream. + return FirebaseStream(host_, auth_, path, &http_); } // FirebaseCall FirebaseCall::FirebaseCall(const String& host, const String& auth, const char* method, const String& path, - const String& data, HTTPClient* http) { - if (!http) { - http = &http_; - } - + const String& data, HTTPClient* http) : http_(http) { String url = makeFirebaseURL(path, auth); - http->setReuse(true); - http->begin(host, kFirebasePort, url, true, kFirebaseFingerprint); + http_->setReuse(true); + http_->begin(host, kFirebasePort, url, true, kFirebaseFingerprint); bool followRedirect = false; if (method == "STREAM") { method = "GET"; - http->addHeader("Accept", "text/event-stream"); + http_->addHeader("Accept", "text/event-stream"); followRedirect = true; } if (followRedirect) { const char* headers[] = {"Location"}; - http->collectHeaders(headers, 1); + http_->collectHeaders(headers, 1); } - int status = http->sendRequest(method, (uint8_t*)data.c_str(), data.length()); + int status = http_->sendRequest(method, (uint8_t*)data.c_str(), data.length()); // TODO: Add a max redirect check if (followRedirect) { while (status == HTTP_CODE_TEMPORARY_REDIRECT) { - String location = http->header("Location"); - http->setReuse(false); - http->end(); - http->setReuse(true); - http->begin(location, kFirebaseFingerprint); - status = http->sendRequest(method, (uint8_t*)NULL, 0); + String location = http_->header("Location"); + http_->setReuse(false); + http_->end(); + http_->setReuse(true); + http_->begin(location, kFirebaseFingerprint); + status = http_->sendRequest("GET", (uint8_t*)NULL, 0); } } @@ -102,7 +99,7 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth, // if not streaming. if (!followRedirect) { - response_ = http->getString(); + response_ = http_->getString(); } } @@ -138,16 +135,17 @@ FirebaseRemove::FirebaseRemove(const String& host, const String& auth, // FirebaseStream FirebaseStream::FirebaseStream(const String& host, const String& auth, - const String& path) - : FirebaseCall(host, auth, "STREAM", path) { + const String& path, + HTTPClient* http) + : FirebaseCall(host, auth, "STREAM", path, "", http) { } bool FirebaseStream::available() { - return http_.getStreamPtr()->available(); + return http_->getStreamPtr()->available(); } FirebaseStream::Event FirebaseStream::read(String& event) { - auto client = http_.getStreamPtr(); + auto client = http_->getStreamPtr(); Event type; String typeStr = client->readStringUntil('\n').substring(7); if (typeStr == "put") { diff --git a/Firebase.h b/Firebase.h index 99927568..aac4e86c 100644 --- a/Firebase.h +++ b/Firebase.h @@ -46,6 +46,8 @@ class Firebase { FirebaseRemove remove(const String& path); // Starts a stream of events that affect object at "path". + // TODO: fix FirebaseStream lifecycle + // https://github.com/esp8266/Arduino/issues/500 FirebaseStream stream(const String& path); private: @@ -69,6 +71,7 @@ class FirebaseError { class FirebaseCall { public: + FirebaseCall() {} FirebaseCall(const String& host, const String& auth, const char* method, const String& path, const String& data = "", @@ -80,13 +83,14 @@ class FirebaseCall { return response_; } protected: - HTTPClient http_; + HTTPClient* http_; FirebaseError error_; String response_; }; class FirebaseGet : public FirebaseCall { public: + FirebaseGet() {} FirebaseGet(const String& host, const String& auth, const String& path, HTTPClient* http = NULL); @@ -100,6 +104,7 @@ class FirebaseGet : public FirebaseCall { class FirebasePush : public FirebaseCall { public: + FirebasePush() {} FirebasePush(const String& host, const String& auth, const String& path, const String& value, HTTPClient* http = NULL); @@ -113,6 +118,7 @@ class FirebasePush : public FirebaseCall { class FirebaseRemove : public FirebaseCall { public: + FirebaseRemove() {} FirebaseRemove(const String& host, const String& auth, const String& path, HTTPClient* http = NULL); }; @@ -120,8 +126,9 @@ class FirebaseRemove : public FirebaseCall { class FirebaseStream : public FirebaseCall { public: + FirebaseStream() {} FirebaseStream(const String& host, const String& auth, - const String &path); + const String& path, HTTPClient* http = NULL); // True if there is an event available. bool available(); @@ -141,7 +148,6 @@ class FirebaseStream : public FirebaseCall { } private: - HTTPClient http_; FirebaseError _error; }; diff --git a/examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino b/examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino index 1f30d68c..c1bd368e 100644 --- a/examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino +++ b/examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino @@ -20,8 +20,8 @@ #include // create firebase client. -Firebase fbase = Firebase("example.firebaseio.com") - .auth("secret_or_token"); +Firebase fbase("example.firebaseio.com") + .auth("secret_or_token"); void setup() { Serial.begin(9600); diff --git a/examples/FirebaseStream_ESP8266/FirebaseStream_ESP8266.ino b/examples/FirebaseStream_ESP8266/FirebaseStream_ESP8266.ino index 92743215..3f24bfec 100644 --- a/examples/FirebaseStream_ESP8266/FirebaseStream_ESP8266.ino +++ b/examples/FirebaseStream_ESP8266/FirebaseStream_ESP8266.ino @@ -24,7 +24,8 @@ #define OLED_RESET 10 Adafruit_SSD1306 display(OLED_RESET); -Firebase fbase = Firebase("publicdata-cryptocurrency.firebaseio.com"); +Firebase fbase("publicdata-cryptocurrency.firebaseio.com"); +FirebaseStream stream; void setup() { Serial.begin(9600); @@ -42,11 +43,11 @@ void setup() { Serial.println(); Serial.print("connected: "); Serial.println(WiFi.localIP()); + stream = fbase.stream("/bitcoin"); } void loop() { - static FirebaseStream stream = fbase.stream("/bitcoin"); if (!stream.error()) { Serial.println("streaming error"); Serial.println(stream.error().message()); @@ -57,7 +58,7 @@ void loop() { auto type = stream.read(event); Serial.print("event: "); Serial.println(type); - if (type != FirebaseEvent::Event::UNKNOWN) { + if (type != FirebaseStream::Event::UNKNOWN) { Serial.print("data: "); Serial.println(event);