B4J Question WebSocket - Run Sub from HTML Function

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using WebSockets in my B4J app and I am trying to work out how to trigger a sub in my B4J project from a HTML page function.

Looking though the tutorial you need to put a sub in the B4J app like:

B4X:
Sub btnCalc_Click (Params As Map)
     Result.SetText("Hello World")
End Sub

And using a button in the HTML page:
HTML:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Web App Hello World!</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
  <link rel="stylesheet" type="text/css" href="index.css" />
  <script src="/b4j_ws.js"></script>
</head>
<body>
  <h1>Web App Hello World!</h1>
  <p id="plog"></p>
  <div id="maindiv">
  First Number: <input id="text1" type="text"></input><br/>
  Second Number: <input id="text2" type="text"></input><br/>
  <button id="btncalc">Calculate</button><br/>
  <p id="result"></p>
  </div>
   <script>
  //connect to the web socket when the page is ready.
  $( document ).ready(function() {
  b4j_connect("/ws");
  });
   </script>
</body>

Everything is working fine so far..

But rather than using a button to trigger the code in the B4J app (btnCalc_Click) is there a way to run the sub in the B4J from a function in the HTML page ?

HTML:
<button onClick="RunFunction();">Click Me</button>

function RunFunction()
{
    // What code can I use here to run the Sub in my B4J app?
}
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…