Links2004 / arduinoWebSockets

arduinoWebSockets
GNU Lesser General Public License v2.1
1.88k stars 556 forks source link

Need to Implement Global Switching Between Wi-Fi and GSM for WebSocket Connections. #910

Open SwapB5632 opened 1 week ago

SwapB5632 commented 1 week ago

@Links2004 I am working on a project using ESP32 and the WebSockets library, where I need to switch between Wi-Fi and GSM networks dynamically. I would like to globally define WEBSOCKETS_NETWORK_TYPE to control whether the WebSocket connection uses Wi-Fi (NETWORK_WIFI) or GSM (NETWORK_GSM) without modifying the core WebSocket library files.

Currently, I am using TinyGSM for GSM and WiFi.h for Wi-Fi. I would like to know the best approach to handle this globally so that I can toggle between Wi-Fi and GSM by simply setting WEBSOCKETS_NETWORK_TYPE in the project.

for reference kindly check the screenshot. image

Links2004 commented 1 week ago

WEBSOCKETS_NETWORK_TYPE is designed to be overridden by global compiler defines. for platform.io this looks like this:

build_flags =
    -D WEBSOCKETS_NETWORK_TYPE=NETWORK_W5100

the part you showing is the auto detection if nothing is defined by the user / compiler flags yet.

SwapB5632 commented 1 week ago

@Links2004 thanks for your response.

but is there any solution to Switch between Wi-Fi and GSM dynamically.

Links2004 commented 1 week ago

what exactly do you mean by dynamically during runtime of the code on a µC? if yes then there is currently no way implemented to do this, since all the network types do is switching the TCP stack at compile time.

the "easiest" way that I see is to write a wrapper class for the TCP server/client that does the switching inside this wrapper.

I really wished that the arduino framework had a "interface" class that defined the TCP server / client functions, but thats not the cases which makes it next to impossible to do this generic for all the different TCP stacks of all the µC out there.

there is a other option for lwip based µC (example ESP), but not sure how easy it is. the lwip framework allows you the add new network interfaces to it. with that the switching will be route based on the lwip side and the "WiFiClient" will happy use any interface for its traffic.