Did you write this existing HTML5 app? Of what does it consist? Is it just the HTML5 front-end? To have an HTML5 app communicate with a B4J server via WebSockets, that HTML5 app must have this line:
<script src="b4j_ws.js"></script>
or something equivalent and then you have to host the b4j_ws.js JavaScript file on your server (in the root of the statics folder as in this example).
In your main module, you would have this code:
Sub Process_Globals
Dim srvr as Server
End Sub
Sub AppStart(Args() as String)
srvr.Initialize("srvr")
srvr.AddWebsocket("/SampleWS", "SampleWS")
srvr.Port = 8000
srvr.Start
StartMessageLoop
End Sub
Then you would create a WebSocketClass module called "SampleWS" as follows:
'WebSocket class
Sub Class_Globals
Private ws As WebSocket
End Sub
Public Sub Initialize
End Sub
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
ws = WebSocket1
Log("SampleWS connected: " & DateTime.Time(DateTime.Now))
ws.RunFunction("ClientGetsString", Array("this is a sample message sent from the server"))
ws.Flush
End Sub
Private Sub WebSocket_Disconnected
Log("SampleWS disconnected: " & DateTime.Time(DateTime.Now))
End Sub
Sub fromUser_LogThisString(params as Map)
Dim sampleString as String = params.Get("sample")
Log("Sample String: " & sampleString)
End Sub
This WebSocket will call a method in the client's browser called "ClientGetsString()" (provided you have written such a function in your HTML5 app) as soon as a connection is established. Additionally, the HTML5 app can raise an event called "fromUser_LogThisString" that will output the String to the Log(). This can be called in JavaScript in your HTML5 app as such:
b4j_raiseEvent("fromUser_LogThisString", {sample:"This is a sample message sent from the client"});
Also, in your client code, you should call
inside your $(document).ready() function in order to connect to your SampleWS WebSocket.
Additionally, you can execute arbitrary code in the client by calling ws.Eval().