B4J Question Websocket: when terminated

wl

Well-Known Member
Licensed User
Longtime User
Hi,

When I call a b4j_connect I understand a new instance of the corresponding websocket class is instantiated.

When does this instance gets destroyed: is there some kind of timeout ?
For example: a websocket gets created (and the WebSocket_Connected is called) but then no call is being made (indefintely) ?

Thanks
 

billzhan

Active Member
Licensed User
Longtime User
WebSocket_Disconnected is something you are looking for, close DB connections in this sub.

This sub may not be called immediately when the connection is broken.
B4X:
Private Sub WebSocket_Disconnected
End Sub
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
WebSocket_Disconnected is something you are looking for, close DB connections in this sub.

This sub may not be called immediately when the connection is broken.
B4X:
Private Sub WebSocket_Disconnected
End Sub

Indeed this method is not called when the connection is not propertly closed.
For a server running 24/7 I guess there must be something to clean up these dangling connections ?

Tx
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
It will be eventually raised. If you want to have more control over it then run a JQuery method with a timer every few seconds. It will cause the logical connection to break if there is no actual connection.

When the connection drops a timed jquery from the client will never reach the server (and would thus make no difference), but I guess you meant that the server is to check (also with a timer) that a heartbeat was received. If so: that's exactly what I am doing at this moment/
 
Upvote 0

billzhan

Active Member
Licensed User
Longtime User
Add a server side event(which does nothing) in a timer. B4J server will try to send a message.


B4X:
Sub Timer1_tick
   ws.Eval("null;",Null)
   ws.flush
End sub
 
Last edited:
Upvote 0

billzhan

Active Member
Licensed User
Longtime User
Remember to disable the timer

B4X:
Private Sub WebSocket_Disconnected
     timer1.enable = False
End Sub
 
Upvote 0

mindful

Active Member
Licensed User
Add a server side event(which does nothing) in a timer. B4J server will try to send a message.


B4X:
Sub Timer1_tick
   ws.Eval("null;",Null)
   ws.flush
End sub

I got faster disconnect times using :
B4X:
Sub Timer1_Tick
  Try
    Dim browserCheck as Future = ws.EvalWithResult("return null", Null)
    Dim getValue as Object = browserCheck.Value
    Log("Client browser reachable")
  Catch
    Log("Client browser NOT reachable")
  End Try
End Sub

The websocket raises the disconnected event faster in my case ... tested with a timer interval of 10 seconds, connecting from my phone and turning Airplane mode ON.
 
Upvote 0
Top