B4R Question How to reliably handle disconnects/unexpected failures with WebSockets?

ByteCounter

Member
Licensed User
Can someone share a SIMPLE example of using WebSockets to communicate between B4R / B4J?

How is WebSockets different from using a normal socket for reliable transmission from code point of view? (i.e. detecting that Wifi connection dropped and reconnecting automatically)

I have seen https://www.b4x.com/android/forum/t...ay-dht11-sensor-data-google-line-chart.67181/ but I'm having trouble understanding the use of the Arduino and Browser classes with respect to the main code.

I want to create something that transfers data between B4R and B4J that doesn't require a broker (so MQTT is not an option) but it must reconnect if Wifi drops (or something else goes wrong). How do you handle this? Any tips on creating robust code?

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

ByteCounter

Member
Licensed User
@Erel thanks for the link. That example seems to be for an Arduino using the Ethernet Shield. Any chance of an ESP8266 example?

If I am using the 8266 with the Wifi to connect via WebSockets I have 2 things to worry about:
1. The WiFi could drop at any time.
2. The WebSocket connection could be interrupted.

The question is how to structure code to detect and gracefully recover from wifi and websockets disconnects/reconnects?

For Wifi I'm thinking of a Timer that fires every couple hundred milliseconds to check ESP8266WiFi.IsConnected and reattempts connection.

For WebSockets the Disconnected event code in WebSocketClient.Initialize could be used to detect connection loss. Actually now that I think about it you probably could do the Wifi check in the WebSocketClient disconnect code?

Is there anything wrong with this approach? Am I missing anything?
 
Upvote 0
Top