rjwats / esp8266-react

A framework for ESP8266 & ESP32 microcontrollers with a React UI
GNU Lesser General Public License v3.0
478 stars 147 forks source link

How to listen to other WebSocket Events #237

Open ronickg opened 3 years ago

ronickg commented 3 years ago

In my application I need to know when the WebSockert Connection closes.

case WS_EVT_DISCONNECT:
  Serial.printf("WebSocket client #%u disconnected\n", client->id());
  break;

This is what i need to listen for but sadly my c++ skills arent that good to implement it in the current application. As i would have to edit the WebSocketTxRx class in order to have a sort of callback to my Service Class. I am using your included WebSockets example.

rjwats commented 3 years ago

Hummm yes - a bit awkward.

I would probably look at introducing a disconnect event callback in WebSocketConnector as a function pointer (in the framework code like you said).

Another valid alternative is to write your own websocket behaviour directly on top of the async server api. I guess it depends on whether you need the event driven stateful service approach or not :)

If you implement the former approach feel free to raise a PR - I'm not finding as much time as I'd like to work on this at the moment.

On Sat, 27 Mar 2021, 11:45 Ronald Goedeke, @.***> wrote:

In my application I need to know when the WebSockert Connection closes.

case WS_EVT_DISCONNECT: Serial.printf("WebSocket client #%u disconnected\n", client->id()); break;

This is what i need to listen for but sadly my c++ skills arent that good to implement it in the current application. As i would have to edit the WebSocketTxRx class in order to have a sort of callback to my Service Class. I am using your included WebSockets example.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rjwats/esp8266-react/issues/237, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKE4VCVFSUKSBMU3QPUDXDTFXANPANCNFSM4Z42XPIQ .

ronickg commented 3 years ago

Okay thx. I'll have a look what i can do and then raise a PR, but I am not that good in c++.

rjwats commented 3 years ago

No pressure to raise a PR :) I was mainly wanting to give you confidence to make changes to the framework - it's far from complete feature wise.

I was thinking instead of a disconnect callback, a general purpose event callback might be better - so any event may be handled. I can't remember what other events the websocket can raise of the top of my head - just a thought.

On Sat, 27 Mar 2021, 13:33 Ronald Goedeke, @.***> wrote:

Okay thx. I'll have a look what i can do and then raise a PR, but I am not that good in c++.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/rjwats/esp8266-react/issues/237#issuecomment-808733916, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKE4VCONUNKWZXJ2ATIYPTTFXNDJANCNFSM4Z42XPIQ .

ronickg commented 3 years ago

Yes that would make the most sense. On the React side of things I also had to add some functionally to the Websockets. Cause in my case I just want to send over an object, without looking at its current state. Not sure if its useful for other projects though.

  sendData = (data: any) => {
    const { ws, connected } = this.state;
    if (connected) {
      ws.json(data);
    }
  };