Skip to content

firebase: fix stream example #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2016
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
42 changes: 20 additions & 22 deletions Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -102,7 +99,7 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,

// if not streaming.
if (!followRedirect) {
response_ = http->getString();
response_ = http_->getString();
}
}

Expand Down Expand Up @@ -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") {
Expand Down
12 changes: 9 additions & 3 deletions Firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 = "",
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -113,15 +118,17 @@ class FirebasePush : public FirebaseCall {

class FirebaseRemove : public FirebaseCall {
public:
FirebaseRemove() {}
FirebaseRemove(const String& host, const String& auth,
const String& path, HTTPClient* http = NULL);
};


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();
Expand All @@ -141,7 +148,6 @@ class FirebaseStream : public FirebaseCall {
}

private:
HTTPClient http_;
FirebaseError _error;
};

Expand Down
4 changes: 2 additions & 2 deletions examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include <Firebase.h>

// 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);
Expand Down
7 changes: 4 additions & 3 deletions examples/FirebaseStream_ESP8266/FirebaseStream_ESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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);

Expand Down