timum-viw / socket.io-client

A socket.io-client implementation for ESP8266 and Arduino
228 stars 90 forks source link

begin("192.168.1.3", 3000,"/news"); URL not working #76

Closed alaa-fouzai closed 4 years ago

alaa-fouzai commented 4 years ago

im using this code to connect to a nodejs server arduino code :

#include <SocketIoClient.h>
#include <Arduino.h>

_#include_ <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;
SocketIoClient webSocket;

void checkHeap() {
  Serial.print("heap: ");
  Serial.println(ESP.getFreeHeap());
}

void ConnectedToIO(const char* payload, size_t length) {
  Serial.printf("connected to webSocket\n");
    webSocket.emit("helloUser", "{ \"id\": \"abc\"}\0");
    delay(1000);

}
void news(const char* payload, size_t length) {
  Serial.printf("news\n");
    checkHeap();
    Serial.print(payload);
    delay(1000);
}

void event(const char* payload, size_t length) {
  USE_SERIAL.printf("got message: %s\n", payload);
}

void setup() {
  USE_SERIAL.begin(115200);

  USE_SERIAL.setDebugOutput(true);

  USE_SERIAL.println();
  USE_SERIAL.println();
  USE_SERIAL.println();
  wifi_set_sleep_type(NONE_SLEEP_T);
  pinMode(LED_BUILTIN, OUTPUT);

  for (uint8_t t = 4; t > 0; t--) {
    USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
    USE_SERIAL.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("*************", "*****************");

  webSocket.on("connect", ConnectedToIO);
  webSocket.on("news", news);
  char path[] = "/news";
  webSocket.begin("192.168.1.3", 3000,path);
}

void loop() {
    webSocket.loop();
  delay(5000);
  webSocket.emit("join", "{ \"id\": \"abc\"}\0");
}

but the path in webSocket.begin("192.168.1.3", 3000,path); doesn't seem to work

my node js code

try {
    const news = io
        .of('/news')
        .on('connection', (socket) => {
            console.log('news connected');
            // socket.emit('item', { news: 'item' });
            socket.on('join', data => {console.log(data.toString())});
        });

} catch (e) {
    console.log(e.toString());
}

every time the esp trys to connect the node js server crashes and i get this error

events.js:200
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:200:27)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:81:21) {
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read'
}

the code works fine when i change the node js code to

io.on('connection', function (client) {
    console.log('Client connected...');

    client.on('join', function (data) {
        console.log(data);
    });
    client.emit('news','hello world ');

    client.on('disconnect', function () {
        console.log('Client disconnected!');
    });
});

but i need the path to separate node js clients from other clients (web app ) so the esp connects to 192.168.1.3:3000/news and other clients connect to 192.168.1.3:3000/webapp i tried playing around with the path but other then removing it entirely or putting the default "/socket.io/?transport=websocket" it always crashes my server the other web app connects fine on 192.168.1.3:3000/webapp only the esp have problems and crashes the server

coma0815 commented 4 years ago

Afaik namespaces are not supported by the library that this library is built on or at least the feature is not fully tested yet. I think you would have to start the implementation there.. https://github.com/Links2004/arduinoWebSockets/issues/520