mWebSockets icon indicating copy to clipboard operation
mWebSockets copied to clipboard

The used network controller should be configurable from the sketch

Open Anton-V-K opened this issue 4 years ago • 15 comments

The approach with selecting the network controller inside the library's config.h is hardly usable in real projects - it is impossible to tweak it for every build.

There are several approaches which can address this issue:

  1. Wrap #define NETWORK_CONTROLLER ETHERNET_CONTROLLER_W5X00 with #ifnded NETWORK_CONTROLLER and #endif so if the build system support global defines, the project will choose the proper network controller.
  2. Do a meaningful guess of the network controller (e.g., when ESP8266 or ESP32 is defined, the chances are high that WiFi is used), so for rigid build systems (like the one in Arduino IDE) the library can be used as is.

Environment info

  • IDE w/version: Arduino IDE 1.8.16
  • Platform/Board: esp8266
  • Network controller (shield or module): WiFi

Anton-V-K avatar Feb 19 '22 23:02 Anton-V-K

I've been thinking about refactoring into templates. No more #define NETWORK_CONTROLLER in config.h. Instead, you will need to specialize the client/server.

WebSocketServer<WifiServer, WiFiClient> server;
WebSocketClient<EthernetClient> client;

Additionally you could use multiple network controllers in a single (more powerful) board.

skaarj1989 avatar Feb 20 '22 12:02 skaarj1989

Initial commit pushed into the meta branch.

skaarj1989 avatar Feb 20 '22 20:02 skaarj1989

thanks for your library, sorry for my noob questions:

  1. WebSocketServer<EthernetServer, EthernetClient> server; will instantiate a server that serves Eth ?

2): did you merge that to the main branch as for sure this would be much better than using #define

  1. I would like to be able to connect my socket server both wifi WiFi and Eth, would that be achievable with your library

thanks for your feedback

infrafast avatar Dec 04 '23 13:12 infrafast

@infrafast

  1. Yes
  2. No
  3. Yes

skaarj1989 avatar Dec 04 '23 13:12 skaarj1989

Thanks for your rapid answer, I guess the main which is more recent has some improvement that meta do not have ? if that's the case would you be ok to merge your code from meta to main after if I sucessfully test templates with my code and provide you feedback, or is anything preventing to merge now ? thanks!

infrafast avatar Dec 04 '23 13:12 infrafast

Why do you insist on merging to master? Is there anything that prevents you from using the meta branch? Using multiple network controllers on a single MCU is an edge case. In two years you are the second one who asked for that feature.

BTW the library (also the meta branch) is tested with autobahn testsuite.

skaarj1989 avatar Dec 04 '23 14:12 skaarj1989

I can leave with that, it's just in term of maintenance, I guess it's better to always stick to the main whenever possible, and TBH I am not so familiar in using git... Quick practical question, I have this line in my platformIO.ini skaarj1989/mWebSockets@^1.5.0 do you mind telling me how I should change it to use the meta branch ?

infrafast avatar Dec 04 '23 14:12 infrafast

Looks like a branch/tag should be put after # character, so you could try: https://github.com/skaarj1989/mWebSockets#meta

skaarj1989 avatar Dec 04 '23 14:12 skaarj1989

great, I am able to compile using the meta branch. things works well, when I use WiFi but if I force the usage of ethernet using the if directive, I got bellow error message when compiling, would you have any idea why?

.pio/libdeps/proto/mWebSockets/src/WebSocket.hpp:298:28: error: passing 'const EthernetClient' as 'this' argument discards qualifiers [-fpermissive]

#if 0 using NetClient = WiFiClient; using NetServer = WiFiServer; #else using NetClient = EthernetClient; using NetServer = EthernetServer; #endif

infrafast avatar Dec 04 '23 18:12 infrafast

Which MCU?

skaarj1989 avatar Dec 04 '23 20:12 skaarj1989

PLATFORM: Espressif 32 (6.4.0) > Seeed Studio XIAO ESP32C3 HARDWARE: ESP32C3 160MHz, 320KB RAM, 4MB Flash PACKAGES:

  • framework-arduinoespressif32 @ 3.20011.230801 (2.0.11)

infrafast avatar Dec 05 '23 11:12 infrafast

Fixed. Could you confirm?

skaarj1989 avatar Dec 05 '23 13:12 skaarj1989

Yes, it works, thanks a lot for your work and support. I understood that my case to have both WiFi and Eth is not the most common so I don't want to bother. Still, would you route me to a possible solution to instantiate the proper object (Eth / WiFi) at run time and not at compilation time, as simple example ? I was thinking about creating a pointer to WebSocketServer and then be able to use the same code however I don't know how to do that with/without template ?

infrafast avatar Dec 05 '23 14:12 infrafast

possible solution to instantiate the proper object (Eth / WiFi) at run time and not at compilation time, as simple example ?

I believe, this is a good question for a separate discussion :)

Anton-V-K avatar Dec 05 '23 14:12 Anton-V-K

OK, added the discussion, I think this issue can therefore be closed ?

infrafast avatar Dec 08 '23 07:12 infrafast