Links2004 / arduinoWebSockets

arduinoWebSockets
GNU Lesser General Public License v2.1
1.88k stars 556 forks source link

Websocket server always calls callback function (Arduino Mega + Ethernet Shield) #221

Open vndlczndr opened 7 years ago

vndlczndr commented 7 years ago

Hi!

I am trying to create a small websocket echo server with this arduinoWebSockets library, but I just cannot make it work. When I start the program (code below) on the Arduino, it just starts calling the callback function endlessly with the WStype_t being 1 (WStype_DISCONNECTED). The code runs on an Arduino Mega with an Ethernet Shield. Edit: I am using the ATMega branch.

The source code:

#include <Ethernet.h>
#include <WebSocketsServer.h>

const byte macAddress[] = { 0x02, 0x52, 0x3B, 0xE1, 0xCA, 0x19 };

const IPAddress IPAddressOfServer(192, 168, 1, 5);
const IPAddress networkMask(255, 255, 255, 255);
const IPAddress defaultGateway(192, 168, 1, 1);
const IPAddress DNSServer(8, 8, 8, 8);

WebSocketsServer webSocketServer(8080);

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {

    switch (type)
    {
    case WStype_CONNECTED:
        Serial.println("Connected");
        break;
    case WStype_DISCONNECTED:
        Serial.println("Connected");
        break;
    case WStype_ERROR:
        Serial.println("Error");
        break;
    case WStype_TEXT:
    case WStype_BIN:
        Serial.println("Got text/bin");
        webSocketServer.sendTXT(4, "OK!");
        break;
    }
}

void setup() {
    Serial.begin(921600);

    Ethernet.begin(macAddress, IPAddressOfServer,
        DNSServer, defaultGateway, networkMask);

    webSocketServer.begin();
    webSocketServer.onEvent(webSocketEvent);

    Serial.println("started!");
}

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

So right now I'm looking for some solution or guidance about what I'm doing wrong. I appreciate any help I can get! Thank you!

G.

Links2004 commented 7 years ago

best way to start is to enable debug.

https://github.com/Links2004/arduinoWebSockets/blob/ATmega/src/WebSockets.h#L30

for AVR you need to get printf working since it is not supported out of the box there. read Adding printf() to Print class in https://playground.arduino.cc/Main/Printf

vndlczndr commented 7 years ago

Thank you for your fast response! When I enable debug mode, the output of the server looks like this: (I commented the "Serial.print" commands in my sketch to show the server output only) https://prnt.sc/g00m23 I also tried to change the maximum client count to 4 in WebSocketsServer.h at line 31, it did not help.

G.

Links2004 commented 7 years ago

the most likely the problem is around: https://github.com/Links2004/arduinoWebSockets/blob/ATmega/src/WebSocketsServer.cpp#L382 for some reason there we get a valide ptr there. then a client is created with this invalide socket, which then is closed again.

vndlczndr commented 7 years ago

Thank you, I'll look into that part and I'll try to find out what causes the problem!

G.

vndlczndr commented 7 years ago

Soo, I managed to discover some (I think) problems and made some (I think) solutions. Your code checks if there is data waiting with the NETWORK_ESP8266, but not with W5100, so the for cycle at 373 will always run. I added these lines after line 373,

#else
    if (_server->available()) {
#endif

and commented the #if - #endif at line 422, 424. This solved the continuous new client issue, but not the next one: http://prntscr.com/gfax5t As the processing of the client headers is one header line at a time, the handleNewClients method will be called multiple times while a client is reading the header lines. To solve this issue, modified the handleClientData method too. I replaced

 int len = client->tcp->available();
            if(len > 0) {

with while (client->tcp->available() > 0) { and this one solved the next issue too. Of course we are not out of the water yet. This time the websocket server works almost fine, but: Every time it gets a packet, as there is new data, _server->available() will be true and it will try to add a new client. Itt will not succeed of course, but the space will be given to this 'not real' new client, and it will try to decode the data: https://justpaste.it/1aq10 This is where I am standing now. Any thoughts?

G.

waz375 commented 5 years ago

Has anybody solved this issue? I keep getting the same continual polling of WStype_DISCONNECTED

daverathbone commented 4 years ago

I got as close as G-Hooper, did anyone fix it? E.g. WebSockets with WS5100 fails with [WSc] Disconnected, ...if not I will re-write it as it's a mess.. way to many #if WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266, Better to have different lib.

CraigHall5 commented 3 years ago

I'm using this library with Teensy-LC. (WebSocketsServer V1.3) I'm having exactly the same repeated disconnect problem. I've made the 2 changes suggested by VendelCzinder, and it does behave much better, but as he mentioned, that is not the full solution. Did anyone find a full solution to the problem?