Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 4276c99

Browse files
authored
Merge branch 'master' into master
2 parents bdfe2bc + f022066 commit 4276c99

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

Diff for: src/Firebase.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth,
9494
const char* method, const std::string& path,
9595
const std::string& data, const std::shared_ptr<FirebaseHttpClient> http) : http_(http) {
9696
std::string path_with_auth = makeFirebaseURL(path, auth);
97+
if ((method == "STREAM") && (path == http->getStreamingPath())){
98+
// already streaming requested path.
99+
return;
100+
}
101+
if (http_->isStreaming()) {
102+
// closing streaming connection.
103+
http_->setReuseConnection(false);
104+
http_->end();
105+
}
97106
http_->setReuseConnection(true);
98107
http_->begin(host, path_with_auth);
99108

@@ -132,11 +141,14 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth,
132141
// if not streaming.
133142
if (!followRedirect) {
134143
response_ = http_->getString();
144+
http_->setStreaming("");
145+
} else {
146+
http_->setStreaming(path);
135147
}
136148
}
137149

138150
FirebaseCall::~FirebaseCall() {
139-
if (http_) {
151+
if (http_ && !http_->isStreaming()) {
140152
http_->end();
141153
}
142154
}

Diff for: src/FirebaseArduino.h

-3
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ class FirebaseArduino {
189189
* You should check success() after calling.
190190
* This changes the state of this object. Once this is called you may start
191191
* monitoring available() and calling readEvent() to get new events.
192-
* WARNING: Currently you cannot make another call while the stream is
193-
* running, otherwise you will crash due to memory issues. See:
194-
* https://github.com/googlesamples/firebase-arduino/issues/48
195192
* \param path The path inside of your db to the node you wish to monitor.
196193
*/
197194
void stream(const String& path);

Diff for: src/FirebaseHttpClient.h

+11
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ class FirebaseHttpClient {
3333

3434
virtual std::string errorToString(int error_code) = 0;
3535

36+
bool isStreaming() const {
37+
return _streaming != "";
38+
}
39+
std::string getStreamingPath() const {
40+
return _streaming;
41+
}
42+
void setStreaming(const std::string& path) {
43+
_streaming = path;
44+
}
3645
protected:
46+
std::string _streaming = "";
47+
3748
static const uint16_t kFirebasePort = 443;
3849
};
3950

Diff for: src/FirebaseHttpClient_Esp8266.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,32 @@
1515
#define USE_ESP_ARDUINO_CORE_2_0_0
1616
#endif
1717

18+
// Firebase now returns `Connection: close` after REST streaming redirection.
19+
//
20+
// Override the built-in ESP8266HTTPClient to *not* close the
21+
// connection if forceReuse it set to `true`.
22+
class ForceReuseHTTPClient : public HTTPClient {
23+
public:
24+
void end() {
25+
if (_forceReuse) {
26+
_canReuse = true;
27+
}
28+
HTTPClient::end();
29+
}
30+
void forceReuse(bool forceReuse) {
31+
_forceReuse = forceReuse;
32+
}
33+
protected:
34+
bool _forceReuse = false;
35+
};
36+
1837
class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
1938
public:
2039
FirebaseHttpClientEsp8266() {}
2140

2241
void setReuseConnection(bool reuse) override {
2342
http_.setReuse(reuse);
43+
http_.forceReuse(reuse);
2444
}
2545

2646
void begin(const std::string& url) override {
@@ -64,7 +84,7 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
6484
}
6585

6686
private:
67-
HTTPClient http_;
87+
ForceReuseHTTPClient http_;
6888
};
6989

7090
FirebaseHttpClient* FirebaseHttpClient::create() {

0 commit comments

Comments
 (0)