8000 first parts of client working · PorterK/arduinoWebSockets@1275914 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1275914

Browse files
committed
first parts of client working
1 parent 790a922 commit 1275914

File tree

3 files changed

+107
-28
lines changed

3 files changed

+107
-28
lines changed

src/WebSocketsClient.cpp

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ void WebSocketsClient::begin(const char *host, uint16_t port, const char * url)
6767
// todo find better seed
6868
randomSeed(millis());
6969
#endif
70+
71+
asyncConnect();
7072
}
7173

7274
void WebSocketsClient::begin(String host, uint16_t port, String url) {
@@ -85,11 +87,12 @@ void WebSocketsClient::beginSSL(String host, uint16_t port, String url, String f
8587
}
8688
#endif
8789

90+
91+
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
8892
/**
8993
* called in arduino loop
9094
*/
9195
void WebSocketsClient::loop(void) {
92-
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
9396
if(!clientIsConnected(&_client)) {
9497

9598
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
@@ -120,37 +123,16 @@ void WebSocketsClient::loop(void) {
120123
}
121124

122125
if(_client.tcp->connect(_host.c_str(), _port)) {
123-
DEBUG_WEBSOCKETS("[WS-Client] connected to %s:%u.\n", _host.c_str(), _port);
124-
125-
_client.status = WSC_HEADER;
126-
127-
// set Timeout for readBytesUntil and readStringUntil
128-
_client.tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
129-
130-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
131-
_client.tcp->setNoDelay(true);
132-
133-
if(_client.isSSL && _fingerprint.length()) {
134-
if(!_client.ssl->verify(_fingerprint.c_str(), _host.c_str())) {
135-
DEBUG_WEBSOCKETS("[WS-Client] certificate mismatch\n");
136-
WebSockets::clientDisconnect(&_client, 1000);
137-
return;
138-
}
139-
}
140-
#endif
141-
142-
// send Header to Server
143-
sendHeader(&_client);
144-
126+
connectedCb();
145127
} else {
146-
DEBUG_WEBSOCKETS("[WS-Client] connection to %s:%u Faild\n", _host.c_str(), _port);
128+
connectFailedCb();
147129
delay(10); //some little delay to not flood the server
148130
}
149131
} else {
150132
handleClientData();
151133
}
152-
#endif
153134
}
135+
#endif
154136

155137
/**
156138
* set callback function
@@ -511,3 +493,89 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
511493
}
512494
}
513495

496+
void WebSocketsClient::connectedCb() {
497+
498+
DEBUG_WEBSOCKETS("[WS-Client] connected to %s:%u.\n", _host.c_str(), _port);
499+
500+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
501+
_client.tcp->onDisconnect(std::bind([](WebSocketsClient * c, AsyncTCPbuffer * obj, WSclient_t * client) -> bool {
502+
DEBUG_WEBSOCKETS("[WS-Server][%d] Disconnect client\n", client->num);
503+
client->status = WSC_NOT_CONNECTED;
504+
client->tcp = NULL;
505+
506+
// reconnect
507+
// c->asyncConnect();
508+
509+
return true;
510+
}, this, std::placeholders::_1, &_client));
511+
#endif
512+
513+
_client.status = WSC_HEADER;
514+
515+
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
516+
// set Timeout for readBytesUntil and readStringUntil
517+
_client.tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
518+
#endif
519+
520+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
521+
_client.tcp->setNoDelay(true);
522+
523+
if(_client.isSSL && _fingerprint.length()) {
524+
if(!_client.ssl->verify(_fingerprint.c_str(), _host.c_str())) {
525+
DEBUG_WEBSOCKETS("[WS-Client] certificate mismatch\n");
526+
WebSockets::clientDisconnect(&_client, 1000);
527+
return;
528+
}
529+
}
530+
#endif
531+
532+
// send Header to Server
533+
sendHeader(&_client);
534+
535+
}
536+
537+
538+
void WebSocketsClient::connectFailedCb() {
539+
DEBUG_WEBSOCKETS("[WS-Client] connection to %s:%u Faild\n", _host.c_str(), _port);
540+
}
541+
542+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
543+
544+
void WebSocketsClient::asyncConnect() {
545+
546+
AsyncClient * tcpclient = new AsyncClient();
547+
548+
549+
if(!tcpclient) {
550+
DEBUG_WEBSOCKETS("[WS-Client] creating AsyncClient class failed!");
551+
return;
552+
}
553+
554+
tcpclient->onConnect(std::bind([](WebSocketsClient * ws , AsyncClient * tcp) {
555+
ws->_client.tcp = new AsyncTCPbuffer(tcp);
556+
if(!ws->_client.tcp) {
557+
DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!");
558+
ws->connectFailedCb();
559+
return;
560+
}
561+
ws->connectedCb();
562+
}, this, std::placeholders::_2));
563+
564+
tcpclient->onError(std::bind([](WebSocketsClient * ws , AsyncClient * tcp) {
565+
ws->connectFailedCb();
566+
567+
// reconnect
568+
ws->asyncConnect();
569+
}, this, std::placeholders::_2));
570+
571+
if(!tcpclient->connect(_host.c_str(), _port)) {
572+
connectFailedCb();
573+
delete tcpclient;
574+
}
575+
576+
}
577+
578+
#endif
579+
580+
581+

src/WebSocketsClient.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ class WebSocketsClient: private WebSockets {
4444
void beginSSL(String host, uint16_t port, String url = "/", String fingerprint = "");
4545
#endif
4646

47+
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
4748
void loop(void);
49+
#else
50+
// Async interface not need a loop call
51+
void loop(void) __attribute__ ((deprecated)) {}
52+
#endif
4853

4954
void onEvent(WebSocketClientEvent cbEvent);
5055

@@ -82,6 +87,13 @@ class WebSocketsClient: private WebSockets {
8287
void sendHeader(WSclient_t * client);
8388
void handleHeader(WSclient_t * client, String * headerLine);
8489

90+
void connectedCb();
91+
void connectFailedCb();
92+
93+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
94+
void asyncConnect();
95+
#endif
96+
8597
/**
8698
* called for sending a Event to the app
8799
* @param type WStype_t

src/WebSocketsServer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ class WebSocketsServer: private WebSockets {
4242
~WebSocketsServer(void);
4343

4444
void begin(void);
45+
4546
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
4647
void loop(void);
4748
#else
4849
// Async interface not need a loop call
49-
void loop(void) __attribute__ ((deprecated)) {
50-
51-
}
50+
void loop(void) __attribute__ ((deprecated)) {}
5251
#endif
5352

5453
void onEvent(WebSocketServerEvent cbEvent);

0 commit comments

Comments
 (0)
0