Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit34357a9

Browse files
committed
Merge pull requestFirebaseExtended#3 from proppy/refactor
firebase: fix stream example
2 parentsc65fef7 +b286b19 commit34357a9

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

‎Firebase.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,44 +55,41 @@ FirebaseRemove Firebase::remove(const String& path) {
5555
}
5656

5757
FirebaseStreamFirebase::stream(const String& path) {
58-
returnFirebaseStream(host_, auth_, path);// stream doesn't reuse http client.
58+
// TODO: create new client dedicated to stream.
59+
returnFirebaseStream(host_, auth_, path, &http_);
5960
}
6061

6162
// FirebaseCall
6263
FirebaseCall::FirebaseCall(const String& host,const String& auth,
6364
constchar* method,const String& path,
64-
const String& data, HTTPClient* http) {
65-
if (!http) {
66-
http = &http_;
67-
}
68-
65+
const String& data, HTTPClient* http) : http_(http) {
6966
String url =makeFirebaseURL(path, auth);
70-
http->setReuse(true);
71-
http->begin(host,kFirebasePort, url,true,kFirebaseFingerprint);
67+
http_->setReuse(true);
68+
http_->begin(host,kFirebasePort, url,true,kFirebaseFingerprint);
7269

7370
bool followRedirect =false;
7471
if (method =="STREAM") {
7572
method ="GET";
76-
http->addHeader("Accept","text/event-stream");
73+
http_->addHeader("Accept","text/event-stream");
7774
followRedirect =true;
7875
}
7976

8077
if (followRedirect) {
8178
constchar* headers[] = {"Location"};
82-
http->collectHeaders(headers,1);
79+
http_->collectHeaders(headers,1);
8380
}
8481

85-
int status =http->sendRequest(method, (uint8_t*)data.c_str(), data.length());
82+
int status =http_->sendRequest(method, (uint8_t*)data.c_str(), data.length());
8683

8784
// TODO: Add a max redirect check
8885
if (followRedirect) {
8986
while (status == HTTP_CODE_TEMPORARY_REDIRECT) {
90-
String location =http->header("Location");
91-
http->setReuse(false);
92-
http->end();
93-
http->setReuse(true);
94-
http->begin(location,kFirebaseFingerprint);
95-
status =http->sendRequest(method, (uint8_t*)NULL,0);
87+
String location =http_->header("Location");
88+
http_->setReuse(false);
89+
http_->end();
90+
http_->setReuse(true);
91+
http_->begin(location,kFirebaseFingerprint);
92+
status =http_->sendRequest("GET", (uint8_t*)NULL,0);
9693
}
9794
}
9895

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

103100
// if not streaming.
104101
if (!followRedirect) {
105-
response_ =http->getString();
102+
response_ =http_->getString();
106103
}
107104
}
108105

@@ -138,16 +135,17 @@ FirebaseRemove::FirebaseRemove(const String& host, const String& auth,
138135

139136
// FirebaseStream
140137
FirebaseStream::FirebaseStream(const String& host,const String& auth,
141-
const String& path)
142-
: FirebaseCall(host, auth,"STREAM", path) {
138+
const String& path,
139+
HTTPClient* http)
140+
: FirebaseCall(host, auth,"STREAM", path,"", http) {
143141
}
144142

145143
boolFirebaseStream::available() {
146-
return http_.getStreamPtr()->available();
144+
return http_->getStreamPtr()->available();
147145
}
148146

149147
FirebaseStream::EventFirebaseStream::read(String& event) {
150-
auto client = http_.getStreamPtr();
148+
auto client = http_->getStreamPtr();
151149
Event type;
152150
String typeStr = client->readStringUntil('\n').substring(7);
153151
if (typeStr =="put") {

‎Firebase.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Firebase {
4646
FirebaseRemoveremove(const String& path);
4747

4848
// Starts a stream of events that affect object at "path".
49+
// TODO: fix FirebaseStream lifecycle
50+
// https://github.com/esp8266/Arduino/issues/500
4951
FirebaseStreamstream(const String& path);
5052

5153
private:
@@ -69,6 +71,7 @@ class FirebaseError {
6971

7072
classFirebaseCall {
7173
public:
74+
FirebaseCall() {}
7275
FirebaseCall(const String& host,const String& auth,
7376
constchar* method,const String& path,
7477
const String& data ="",
@@ -80,13 +83,14 @@ class FirebaseCall {
8083
return response_;
8184
}
8285
protected:
83-
HTTPClient http_;
86+
HTTPClient* http_;
8487
FirebaseError error_;
8588
String response_;
8689
};
8790

8891
classFirebaseGet :publicFirebaseCall {
8992
public:
93+
FirebaseGet() {}
9094
FirebaseGet(const String& host,const String& auth,
9195
const String& path, HTTPClient* http =NULL);
9296

@@ -100,6 +104,7 @@ class FirebaseGet : public FirebaseCall {
100104

101105
classFirebasePush :publicFirebaseCall {
102106
public:
107+
FirebasePush() {}
103108
FirebasePush(const String& host,const String& auth,
104109
const String& path,const String& value, HTTPClient* http =NULL);
105110

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

114119
classFirebaseRemove :publicFirebaseCall {
115120
public:
121+
FirebaseRemove() {}
116122
FirebaseRemove(const String& host,const String& auth,
117123
const String& path, HTTPClient* http =NULL);
118124
};
119125

120126

121127
classFirebaseStream :publicFirebaseCall {
122128
public:
129+
FirebaseStream() {}
123130
FirebaseStream(const String& host,const String& auth,
124-
const String &path);
131+
const String&path, HTTPClient* http =NULL);
125132

126133
// True if there is an event available.
127134
boolavailable();
@@ -141,7 +148,6 @@ class FirebaseStream : public FirebaseCall {
141148
}
142149

143150
private:
144-
HTTPClient http_;
145151
FirebaseError _error;
146152
};
147153

‎examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include<Firebase.h>
2121

2222
// create firebase client.
23-
Firebase fbase = Firebase("example.firebaseio.com")
24-
.auth("secret_or_token");
23+
Firebasefbase("example.firebaseio.com")
24+
.auth("secret_or_token");
2525

2626
voidsetup() {
2727
Serial.begin(9600);

‎examples/FirebaseStream_ESP8266/FirebaseStream_ESP8266.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#defineOLED_RESET10
2525
Adafruit_SSD1306display(OLED_RESET);
2626

27-
Firebase fbase = Firebase("publicdata-cryptocurrency.firebaseio.com");
27+
Firebasefbase("publicdata-cryptocurrency.firebaseio.com");
28+
FirebaseStream stream;
2829

2930
voidsetup() {
3031
Serial.begin(9600);
@@ -42,11 +43,11 @@ void setup() {
4243
Serial.println();
4344
Serial.print("connected:");
4445
Serial.println(WiFi.localIP());
46+
stream = fbase.stream("/bitcoin");
4547
}
4648

4749

4850
voidloop() {
49-
static FirebaseStream stream = fbase.stream("/bitcoin");
5051
if (!stream.error()) {
5152
Serial.println("streaming error");
5253
Serial.println(stream.error().message());
@@ -57,7 +58,7 @@ void loop() {
5758
auto type = stream.read(event);
5859
Serial.print("event:");
5960
Serial.println(type);
60-
if (type !=FirebaseEvent::Event::UNKNOWN) {
61+
if (type !=FirebaseStream::Event::UNKNOWN) {
6162
Serial.print("data:");
6263
Serial.println(event);
6364

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp