B4J Question WebSocket - RunFunction

aaronk

Well-Known Member
Licensed User
Hi,

Just wondering what the trick is to run the web socket RunFunction function?

I have a function on my HTML page like:

HTML:
<script>
    jQuery(document).ready(function() {
        test();
    });

    function test(){
        alert("testing");
    }
</script>
When I run my HTML code it will alert 'testing' since it's calling that function on page load, which is fine while I am testing it.

In my B4J app I am trying to call this function using a web socket like:

B4X:
Sub btnCalc_Click (Params As Map)
  
    ws.RunFunction("test", Null)
  
End Sub
The problem I have is when I run the app it comes up with the error:

B4X:
Parsing code.                           0.05
Compiling code.                         Error
Error compiling program.
Error description: Too many parameters.
Occurred on line: 24
ws.RunFunction("test", Null)
Word: _c
From what I understand there is too many parameters in the RunFunction function part in my code.

I tried doing:

B4X:
Sub btnCalc_Click (Params As Map)
  
    ws.RunFunction("test")
  
End Sub
Now when I run the app it says:

B4X:
Parsing code.                           0.03
Compiling code.                         Error
Error compiling program.
Error description: Missing parameter.
Occurred on line: 24
ws.RunFunction("test")
Word: )
How can I trigger the JavaScript Function from a web Socket, is this the correct way in doing it ?
 

aaronk

Well-Known Member
Licensed User
ws.RunFunction("test",Array As Object(Null))
Still get the same error.

B4X:
Parsing code.                           0.14
Compiling code.                         Error
Error compiling program.
Error description: Too many parameters.
Occurred on line: 24
ws.RunFunction("test",Array As Object(Null))
Word: array
(Running this with B4J version 2.50)
 

Daestrum

Well-Known Member
Licensed User
Are you including the b4j_ws.js file in your page, and then on doc.ready calling b4j_connect("??") ?
 

aaronk

Well-Known Member
Licensed User
I made simple test based on the webapp hello world demo. (b4j 2.5 &2.8)

B4X:
ws.RunFunction("test", Null)  'working
ws.RunFunction("test",Array As Object(Null))  'working
Something else may cause this error
When I run your example it seems to work.
I then copied your 'HelloWorld' module code into my project and it then comes up with the same error saying there isToo many parameters.


Are you including the b4j_ws.js file in your page, and then on doc.ready calling b4j_connect("??") ?
Yes I have that in my www folder and am calling it.

HTML:
$( document ).ready(function() {
    b4j_connect("/ws");
});
The problem is that it won't compile the B4J app.

There must be something else in my project that is causing this error and will need to now look through my other modules to see what might be the cause.
At least I now know that it should work, and will now look into it and find out what the cause might be.
 

aaronk

Well-Known Member
Licensed User
Worked it out..

In my Main Module I had the code:

B4X:
Sub AppStart (Args() As String)
    srvr.Initialize("srvr")
    srvr.AddWebSocket("/ws", "WebSocket")
    srvr.Port = 51042
    srvr.Start
    StartMessageLoop
End Sub
As you will see the Module it's going to use is 'WebSocket'

Since in that module it's calling the WebSocket (Private ws As WebSocket) you can't have the class module called 'WebSocket'.

Soon as I changed the Class Module from 'WebSocket' to something else such as 'WebSock' and then change the code in the Main module to:

B4X:
Sub AppStart (Args() As String)
    srvr.Initialize("srvr")
    srvr.AddWebSocket("/ws", "WebSock")
    srvr.Port = 51042
    srvr.Start
    StartMessageLoop
End Sub
Everything worked as it should. Something so simple to fix but always the last thing to check.

Thanks for everyone's help.
 
Top