Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Callback not working ? #12

Closed
himijendrix24 opened this issue May 29, 2019 · 4 comments
Closed

Event Callback not working ? #12

himijendrix24 opened this issue May 29, 2019 · 4 comments
Assignees
Labels
resolved the issue was resolved

Comments

@himijendrix24
Copy link

Hello, I'm using an esp32 (olimex esp32 gateway) as a websocket serve which a local html5 page connects to. Its wired via ethernet.
If the webpage crahes, I want it to reconnect on relaunch.

I'm using a flag to store the connection state of the websocket,
which is updated in the websocket event callback.
However these are not called on connection.
Serial.println("TEMP: connected"); is printing fine and javascript ws.onopen returns true.

Whats wrong?
How can I detect the websocket state here?

#include <ArduinoWebsockets.h>
#include <ETH.h>

bool eth_connected = false;
IPAddress local_IP(192, 168, 0, 50);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(9, 9, 9, 9);
IPAddress secondaryDNS(8, 8, 8, 8);

bool ws_connected = false;
using namespace websockets;
WebsocketsServer server;
WebsocketsClient client;

// Ethernet callbacks
void WiFiEvent(WiFiEvent_t event)
{
  switch (event) {
    case SYSTEM_EVENT_ETH_START:
      Serial.println("ETH Started");
      //set eth hostname here
      ETH.setHostname("esp32-ethernet");
      break;
    case SYSTEM_EVENT_ETH_CONNECTED:
      Serial.println("ETH Connected");
      break;
    case SYSTEM_EVENT_ETH_GOT_IP:
      Serial.print("ETH MAC: ");
      Serial.print(ETH.macAddress());
      Serial.print(", IPv4: ");
      Serial.print(ETH.localIP());
      if (ETH.fullDuplex()) {
        Serial.print(", FULL_DUPLEX");
      }
      Serial.print(", ");
      Serial.print(ETH.linkSpeed());
      Serial.println("Mbps");
      eth_connected = true;
      break;
    case SYSTEM_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      break;
    case SYSTEM_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      eth_connected = false;
      break;
    default:
      break;
  }
}

void onMessageCallback(WebsocketsMessage message) {
  Serial.print("Got Message: ");
  Serial.println(message.data());
}

void onEventsCallback(WebsocketsEvent event, String data) {
  if (event == WebsocketsEvent::ConnectionOpened) {
    Serial.println("Connnection Opened");
    ws_connected = true;
  } else if (event == WebsocketsEvent::ConnectionClosed) {
    Serial.println("Connnection Closed");
    ws_connected = false;
  } else if (event == WebsocketsEvent::GotPing) {
    Serial.println("Got a Ping!");
  } else if (event == WebsocketsEvent::GotPong) {
    Serial.println("Got a Pong!");
  }
}

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

  // Connect ethernet
  WiFi.onEvent(WiFiEvent);
  ETH.begin();
  ETH.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);

  server.listen(80);
  Serial.print("Is server live? ");
  Serial.println(server.available());
}

void loop() {
  if (eth_connected) {

  // ethernet connected, wait for websocket
    if (ws_connected == true) {
      if (client.available()) {
        WebsocketsMessage msg = client.readBlocking();
        Serial.print("Got Message: ");
        Serial.println(msg.data());
      }
    }

    else {
      Serial.println("no websocket client, waiting for connection ...");
      // Setup Callbacks
      client.onMessage(onMessageCallback);
      client.onEvent(onEventsCallback);
      // wait for client (blocking ...)
      client = server.accept();
      Serial.println("TEMP: connected");
    }
    // polling also needed to detect close state ?
    client.poll();
  }

  else {
    ETH.begin();
    ETH.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);
    delay(3000);
  }
}
@himijendrix24
Copy link
Author

And another problem:
Sending from client (javascript) to server (esp32) works fine.

But if I send something from server to client it is rejected and returned as
"�⸮Masked frame from server"
and javascript raises an error:
'ws://192.168.0.50/websocket' failed: A server must not mask any frames that it sends to the client.

@gilmaimon
Copy link
Owner

It seems like you are setting the callbacks before you accept the client. The client that is returned from server.accept() and the one that is there before is not the same client. The callbacks are not saved.

Regarding the masking error message, this was addressed in the last patch. Can you make sure you are using the latest version?

Thank you for the issue, I am currently not marking it as a bug until you will be certain that this issue persists with the lastest version of the library. Also, I will be able to explain more in depth in a few days.

Gil.

@gilmaimon gilmaimon self-assigned this May 29, 2019
@gilmaimon
Copy link
Owner

I believe this was resolved and is a usage error rather than a bug. Please let me know the status soon, otherwise I will close the issue due to inactivity.

Gil.

@gilmaimon gilmaimon added the resolved the issue was resolved label Jun 5, 2019
@gilmaimon
Copy link
Owner

Closed due to inactivity,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolved the issue was resolved
Projects
None yet
Development

No branches or pull requests

2 participants