rWebSocketClient library is a WebSocket client implementation. It is based on this open source project: https://github.com/brandenhall/Arduino-Websocket
The library is similar to B4X WebSocketHandler class: https://www.b4x.com/android/forum/threads/websocket-client-library.40221/#content
This means that it is intended to be used with B4J WebSocket servers.
Using it is simple:
1. Create a client object such as EthernetSocket.
2. Call Connect to connect to the server:
3. Send events to the server with SendEventToServer:
This will cause the Btn_StateChanged sub on the server to be raised:
4. The server can send events to the client:
This will cause the WebSocket_NewMessage event to be raised on the Arduino:
So now it is easy to control the Arduino from the browser!
The connections diagram for this example:
The projects and library are attached. The B4R project requires B4R v1.00 beta 8+.
The library is similar to B4X WebSocketHandler class: https://www.b4x.com/android/forum/threads/websocket-client-library.40221/#content
This means that it is intended to be used with B4J WebSocket servers.
Using it is simple:
1. Create a client object such as EthernetSocket.
2. Call Connect to connect to the server:
B4X:
websocket.ConnectIp(ethClient.Stream, serverIp, serverPort, path)
3. Send events to the server with SendEventToServer:
B4X:
websocket.SendEventToServer("Btn_StateChanged", Array As String("btn", 0, "state", Not(State)))
This will cause the Btn_StateChanged sub on the server to be raised:
B4X:
'b4j
Sub Btn_StateChanged (Params As Map)
Dim id As String = "arduino_btn" & Params.Get("btn")
Dim state As Boolean = Params.Get("state")
For Each br As Browser In Main.ConnectedBrowsers.Keys
CallSubDelayed3(br, "UpdateButton", id, state)
Next
End Sub
4. The server can send events to the client:
B4X:
'Must be an array of strings and the parameters cannot include quotes.
ws.RunFunction("Led", Array As String(LedId.SubString("arduino_led".Length), State))
ws.Flush
This will cause the WebSocket_NewMessage event to be raised on the Arduino:
B4X:
Sub WebSocket_NewMessage (FunctionName As String, Params() As String)
Select FunctionName
Case "ServerTime"
Log("Server time: ", Params(0))
Case "Led"
leds(Params(0)).DigitalWrite(Params(1) = "true")
End Select
End Sub
So now it is easy to control the Arduino from the browser!
The connections diagram for this example:
The projects and library are attached. The B4R project requires B4R v1.00 beta 8+.