ionic3 and websocket arduino server

Hi.

I want to use a websocket server on arduino to send data to an Ionic app.

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet2.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include "WebSocketServer.h"

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF};
IPAddress ip(192, 168, 0, 110);
WebSocketServer wsServer(8080);


void setup() {
  Serial.begin(9600);
  
  Ethernet.begin(mac, ip);
  delay(500);
  Serial.print("Ip address; "); Serial.println(Ethernet.localIP());

  wsServer.setOnErrorCallback(onError);
  wsServer.setOnOpenCallback(onOpen);
  wsServer.setOnMessageCallback(onMessage);
  wsServer.begin();
}

void loop() { 
  wsServer.listen();
}

void onError(eWebSocketError error) {
  Serial.print(F("ERROR 0x")); Serial.println(error, HEX);
}

void onOpen(EthernetClient &client) {
  //Serial.print(F("New client: ")); Serial.println(client.getRemoteIP());
  char msg[] = "Hello from server!";
  wsServer.send(client, msg, strlen(msg));
}

void onMessage(EthernetClient &client, const char *data, uint16_t length) {
  Serial.print(F("[")); Serial.print(client.getRemoteIP()); Serial.print(F("] "));
  Serial.println(data);
}

I use this websocket server library for arduino:

ArduinoWebSocketServer

I enable debugging and handshake display by defining in header file:

//debugguer macro
#define _DEBUG
#define _PRINT_HANDSHAKE

then I run the example. It work with the example provide.

Now, when I try to use it with an ionic websocket client, the arduino froze.

By now, the only thing that I've been able to know is that it froze somewhere in _handleRequest(EthernetClient &client),

This function is call in WebSocketServer::listen(), and I put a serial print after that is never call with ionic client.

that's what is printed on serial monitor:

Ip address; 192.168.0.110
[INFO] New client: 192.168.0.102
[Line #0] GET / HTTP/1.1
[Line #1] Host: 192.168.0.110:8080
[Line #2] Connection: Upgrade
[Line #3] Pragma: no-cache
[Line #4] Cache-Control: no-cache
[Line #5] Upgrade: websocket
[Line #6] Origin: http://192.168.0.101:8100
[Line #7] Sec-WebSocket-Version: 13
[Line #8] User-Agent: Mozilla/5.0 (Linux; Android 7.0; Moto G (5) Build/NPPS25.137-93-8; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 Mobile Safari/537.36
[Line #9] Accept-Encoding: gzip, deflate
[Line #10] Accept-Language: fr-CA,en-US;q=0.9
[Line #11] X-Requested-With: io.ionic.starter
[Line #12] Sec-WebSocket-

And, it get at least to call _onOpen function, it is receive by client.

I join typescript file used in the ionic app, renamed .txt instead of .ts (pear apps the furum should add the .ts extension in the attachment rules... suggestion...)

I have no clue where to dig right now...

thank
Nitrof

configs-inputs.txt (1.12 KB)

socket.txt (893 Bytes)

websocket.txt (2.29 KB)

Ok . some development... but not much progress...

Still unable to exit _handleRequest function. I tried to put while(1) loop to be able to go the further as possible and have the time to serial print a stamp... it is bugging there...

It is even more strange, it bug on "ionic cordova run android", and on real device... but it run ok ok ionic serve (emulator)...

I cant tel if the bug come for the ionic side or the arduino side... but it make the ardiuno froze so...

wish anyone have an idea what to tried...