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

Commit f022066

Browse files
authored
Merge pull request #294 from proppy/fix-stream
FirebaseArduino: allow mixing stream and non-stream commands
2 parents c73ca0e + d62b9cb commit f022066

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/Firebase.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth,
9292
const char* method, const std::string& path,
9393
const std::string& data, FirebaseHttpClient* http) : http_(http) {
9494
std::string path_with_auth = makeFirebaseURL(path, auth);
95+
if ((method == "STREAM") && (path == http->getStreamingPath())){
96+
// already streaming requested path.
97+
return;
98+
}
99+
if (http_->isStreaming()) {
100+
// closing streaming connection.
101+
http_->setReuseConnection(false);
102+
http_->end();
103+
}
95104
http_->setReuseConnection(true);
96105
http_->begin(host, path_with_auth);
97106

@@ -130,11 +139,14 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth,
130139
// if not streaming.
131140
if (!followRedirect) {
132141
response_ = http_->getString();
142+
http_->setStreaming("");
143+
} else {
144+
http_->setStreaming(path);
133145
}
134146
}
135147

136148
FirebaseCall::~FirebaseCall() {
137-
if (http_) {
149+
if (http_ && !http_->isStreaming()) {
138150
http_->end();
139151
}
140152
}
@@ -189,6 +201,9 @@ FirebaseStream::FirebaseStream(const std::string& host, const std::string& auth,
189201
}
190202

191203
bool FirebaseStream::available() {
204+
if (http_->getStreamPtr() == nullptr) {
205+
return false;
206+
}
192207
return http_->getStreamPtr()->available();
193208
}
194209

src/FirebaseArduino.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ void FirebaseArduino::stream(const String& path) {
131131
}
132132

133133
bool FirebaseArduino::available() {
134+
if (http_->getStreamPtr() == nullptr) {
135+
return false;
136+
}
134137
return http_->getStreamPtr()->available();
135138
}
136139

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);

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

0 commit comments

Comments
 (0)