B4J Question WebSockets and Resumable Subs

mindful

Active Member
Licensed User
Hello, in my ABMaterial application in each of my server websocket classes I have a 'heartbeat' timer.

All I do in the timer is send a js command and expect a correct answer. I am using this timer to hurry up the disconnect event in case the connection is left hanging. I initialize the timer in the Initialize sub, enable the timer in the Connected sub and disable the timer in the Disconnected sub.

Can I safely replace this timer with resumable sub using Sleep ?

I will do something like:
B4X:
Sub StartHeartBeat()
    Do While ws.Open
        Sleep (5000)
        VerifyIfBrowserIsAlive
    Loop
End Sub
And in Connected sub I will call the StartHeartBeat.
 

mindful

Active Member
Licensed User
@Erel it works correctly. Just needed a confirmation that I can safely replace timers with resumable subs.

Anyway I encountered a problem with Wait For, I don't know if it is by design or if it a bug so I'll explain here:
In a class if I have a resumable sub with Wait For xxxx_Event and I don't have a sub for that event if I call SubExists it will return true even if I didn't declare that sub just using Wait For. So if using Wait For which waits for xxxx_Event in an resumable sub and I don't create a separate sub for that xxxx_Event calling SubExists(Me, "xxxx_Event") returns true. Is this a bug or is by design !?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is by design. Otherwise you will not be able to use Wait For with many of the events without adding stub subs as they use SubExists (or a similar Java method) to check whether to raise the event or not.

Practically it shouldn't matter (in almost all cases) from the library developer perspective whether you are using Wait For or a sub to handle the event.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…