-->
Page 1 of 4

WebSocket Server ASYNC

PostPosted: Sat Nov 11, 2017 7:43 am
by Josep112
I need a WebSocket Server in Esp Async, I can not get it to work and I do not find an example of the server where I'm going wrong?



#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESPAsyncTCP.h>
#include <WebSocketsServer.h>


WebSocketsServer webSocket = WebSocketsServer(81);

const char* ssid = "*****";
const char* password = "********";
String texto;

void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.printf("WiFi Failed!\n");
return;
}
Serial.printf("WiFi Connected!\n");
Serial.println(WiFi.localIP());
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
Serial.printf(" >>> [WebSockets] Event(%d, %d, ...)\r\n", num, type);
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Desconectado!\r\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf(">>> [WebSockets] [%u] Conectado IP: %d.%d.%d.%d url: %s\r\n", num, ip[0], ip[1], ip[2], ip[3], payload);

Serial.println("Passou pelo conect do WEBSOCKET");
}
break;

case WStype_TEXT:
Serial.printf(">>> [WebSockets] [%u] Recebendo comando: %s\r\n", num, payload);
break;
}
}


void loop()
{
webSocket.loop();
}

Re: WebSocket Server ASYNC

PostPosted: Sat Nov 11, 2017 11:23 am
by Pablo2048
You got it totally wrong - if you use AsyncTCP, then use asyncwebserver also. It has websocket plugin (see https://github.com/me-no-dev/ESPAsyncWebServer ).

Re: WebSocket Server ASYNC

PostPosted: Tue Nov 14, 2017 5:04 am
by Josep112
Well I do not know if I understood it very well, because I need a websocket server in esp, I'm going to do some tests thanks for the help

Re: WebSocket Server ASYNC

PostPosted: Tue Nov 14, 2017 6:01 am
by Pablo2048
If you need websocket server in async mode, the simplest way is to use ESPAsyncWebServer which contain websocket plugin. Your'e not using async method in your code because of websocket.loop() in your loop() (async method are async so no more .loop() things...) I hope that your'e not a noob - async things are more complicated and require more knowledge than simlpe sync things from core...