khoih-prog / AsyncWebServer_Teensy41

Async HTTP/WebSocket Server Library for Teensy 4.1 using QNEthernet. This library is one of the Async libraries to support T4.1, such as AsyncHTTPRequest_Generic, AsyncHTTPSRequest_Generic, AsyncMQTT_Generic, AsyncWebServer_Teensy41, AsyncUDP_Teensy41, AsyncDNSServer_Teensy41, etc. Now supporting using CString to save heap to send very large data
GNU General Public License v3.0
9 stars 6 forks source link

Websocket don´t work #4

Closed Mike0675 closed 1 year ago

Mike0675 commented 1 year ago

Describe the bug

I can´t use WebSockets with Async_WebServer_Teensy41.

I adopted a Sketch from ESP32 where it works well.

With Teensy 4.1 the Connection establishes but send no Data to the Client (HRESULT: 0x80072F78).

Im using Windows10, "Websocket Debug Tools", Wireshark

Arduino IDE version 1.8.19 Teensyduino Core v1.58 beta3 QNEthernet library version v0.17.0

Steps to Reproduce

I used Async_AdvancedWebServer Example and added:

AsyncWebSocket ws("/ws");

void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *WS_Client, AwsEventType type, void *arg, uint8_t *data, size_t len) {

    if (type == WS_EVT_CONNECT) {

        Serial.println("Websocket client connection received");
        WS_Client->text("Hello from ESP32 Server");
    } else if (type == WS_EVT_DISCONNECT) {
        Serial.println("Client disconnected");

    }
}`

In SETUP:

ws.onEvent(onWsEvent);
server.addHandler(&ws);`

In LOOP:

if (millis() > timer + 5000) {
    ws.textAll(String(millis() / 1000));
    timer = millis();
}

Expected behavior

The Server should send back "Hello from Teensy 4.1 Server". Every 5s the Server should send millis() / 1000.

Actual behavior

With "Websocket Debug Tools" I get (HRESULT: 0x80072F78) Using Wireshark it seems the OPCODE for Websocket Connection is Wrong.

khoih-prog commented 1 year ago

Please post the MRE, as well as all the tests procedures / results so far, to save anybody time to duplicate the issue.

khoih-prog commented 1 year ago

Also try to adapt the example from sibling library Async_WebSocketsServer, written for RP2040W AsyncWebServer_RP2040W

khoih-prog commented 1 year ago

Hi @Mike0675

I just adapted the Async_WebSocketsServer code and posted in this library as Async_WebSocketsServer for Teensy4_1

The WebSockets Server is working correctly, meaning the WS protocol is handled correctly in this library

Selection_259


The terminal debug is

Async_WebSocketsServer on TEENSY 4.1

Starting Async_WebSocketsServer on TEENSY 4.1
 with Teensy4.1 QNEthernet
AsyncWebServer_Teensy41 v1.6.1
Initialize Ethernet using DHCP => Connected! IP address:192.168.2.119
ws[Server: /ws][ClientID: 1] WSClient connected
ws[Server: /ws][ClientID: 1] text-message[len: 13]: Hello, Server
ws[Server: /ws][ClientID: 1] WSClient disconnected
ws[Server: /ws][ClientID: 2] WSClient connected
ws[Server: /ws][ClientID: 2] text-message[len: 13]: Hello, Server
ws[Server: /ws][ClientID: 2] WSClient disconnected
ws[Server: /ws][ClientID: 3] WSClient connected
ws[Server: /ws][ClientID: 3] text-message[len: 13]: Hello, Server

The WebSockets Client code using python used to test

https://github.com/khoih-prog/AsyncWebServer_Teensy41/blob/4f5238a8df315658720bd843c48ac57b39a4a8af/examples/Async_WebSocketsServer/WSClient_Python/WSClient.py#L1-L15

Just modify to match the IP of the server. In this case 192.168.2.119