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

FirebaseArduino: allow mixing stream and non-stream commands #294

Merged
merged 1 commit into from
Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth,
const char* method, const std::string& path,
const std::string& data, FirebaseHttpClient* http) : http_(http) {
std::string path_with_auth = makeFirebaseURL(path, auth);
if ((method == "STREAM") && (path == http->getStreamingPath())){
// already streaming requested path.
return;
}
if (http_->isStreaming()) {
// closing streaming connection.
http_->setReuseConnection(false);
http_->end();
}
http_->setReuseConnection(true);
http_->begin(host, path_with_auth);

Expand Down Expand Up @@ -130,11 +139,14 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth,
// if not streaming.
if (!followRedirect) {
response_ = http_->getString();
http_->setStreaming("");
} else {
http_->setStreaming(path);
}
}

FirebaseCall::~FirebaseCall() {
if (http_) {
if (http_ && !http_->isStreaming()) {
http_->end();
}
}
Expand Down Expand Up @@ -189,6 +201,9 @@ FirebaseStream::FirebaseStream(const std::string& host, const std::string& auth,
}

bool FirebaseStream::available() {
if (http_->getStreamPtr() == nullptr) {
return false;
}
return http_->getStreamPtr()->available();
}

Expand Down
3 changes: 3 additions & 0 deletions src/FirebaseArduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ void FirebaseArduino::stream(const String& path) {
}

bool FirebaseArduino::available() {
if (http_->getStreamPtr() == nullptr) {
return false;
}
return http_->getStreamPtr()->available();
}

Expand Down
3 changes: 0 additions & 3 deletions src/FirebaseArduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ class FirebaseArduino {
* You should check success() after calling.
* This changes the state of this object. Once this is called you may start
* monitoring available() and calling readEvent() to get new events.
* WARNING: Currently you cannot make another call while the stream is
* running, otherwise you will crash due to memory issues. See:
* https://github.com/googlesamples/firebase-arduino/issues/48
* \param path The path inside of your db to the node you wish to monitor.
*/
void stream(const String& path);
Expand Down
11 changes: 11 additions & 0 deletions src/FirebaseHttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ class FirebaseHttpClient {

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

bool isStreaming() const {
return _streaming != "";
}
std::string getStreamingPath() const {
return _streaming;
}
void setStreaming(const std::string& path) {
_streaming = path;
}
protected:
std::string _streaming = "";

static const uint16_t kFirebasePort = 443;
};

Expand Down