async client working

This commit is contained in:
Markus Sattler
2016-01-29 13:16:02 +01:00
parent 1275914c86
commit 76853c7a73
2 changed files with 14 additions and 7 deletions

View File

@ -54,8 +54,8 @@
// select Network type based // select Network type based
#ifdef ESP8266 #ifdef ESP8266
//#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266 #define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266
#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266_ASYNC //#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266_ASYNC
#else #else
#define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100 #define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
#endif #endif

View File

@ -67,8 +67,9 @@ void WebSocketsClient::begin(const char *host, uint16_t port, const char * url)
// todo find better seed // todo find better seed
randomSeed(millis()); randomSeed(millis());
#endif #endif
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
asyncConnect(); asyncConnect();
#endif
} }
void WebSocketsClient::begin(String host, uint16_t port, String url) { void WebSocketsClient::begin(String host, uint16_t port, String url) {
@ -504,7 +505,7 @@ void WebSocketsClient::connectedCb() {
client->tcp = NULL; client->tcp = NULL;
// reconnect // reconnect
// c->asyncConnect(); c->asyncConnect();
return true; return true;
}, this, std::placeholders::_1, &_client)); }, this, std::placeholders::_1, &_client));
@ -543,18 +544,24 @@ void WebSocketsClient::connectFailedCb() {
void WebSocketsClient::asyncConnect() { void WebSocketsClient::asyncConnect() {
DEBUG_WEBSOCKETS("[WS-Client] asyncConnect...\n");
AsyncClient * tcpclient = new AsyncClient(); AsyncClient * tcpclient = new AsyncClient();
if(!tcpclient) { if(!tcpclient) {
DEBUG_WEBSOCKETS("[WS-Client] creating AsyncClient class failed!"); DEBUG_WEBSOCKETS("[WS-Client] creating AsyncClient class failed!\n");
return; return;
} }
tcpclient->onDisconnect([](void *obj, AsyncClient* c) {
c->free();
delete c;
});
tcpclient->onConnect(std::bind([](WebSocketsClient * ws , AsyncClient * tcp) { tcpclient->onConnect(std::bind([](WebSocketsClient * ws , AsyncClient * tcp) {
ws->_client.tcp = new AsyncTCPbuffer(tcp); ws->_client.tcp = new AsyncTCPbuffer(tcp);
if(!ws->_client.tcp) { if(!ws->_client.tcp) {
DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!"); DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!\n");
ws->connectFailedCb(); ws->connectFailedCb();
return; return;
} }