B4J Question WebSocket disconnects on Firefox/Safari, not IE/Chrome

lte5000

Member
Licensed User
Longtime User
Hi,

I am running a B4J webapp server on one of my PCs.

I've had Internet Explorer and Chrome clients connected for a day or so with no issues.

However, when I connect with Firefox or Safari, the websocket session will almost always terminate in less than 10 minutes.

The B4J server will fire the WebSocket_Disconnected event.

On the client side, I added this code to the b4j_ws.js file:

B4X:
  b4j_ws.onclose = function(){
     console.log('websocket disconnected!');
     //try to reconnect in 10 seconds
     setTimeout(function(){b4j_connect(absolutePath);}, 10000);
  };

However, this function never fires in the client, so the client will never know that the websocket session has terminated.

The problem is very puzzling to me because it seemingly only occurs in certain browsers.
I can try to post more code if needed.

Thanks for your input!
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Try changing the signature to
B4X:
b4j_ws.onclose = function(event)
{....}

It works for me. Also, are you implementing a heartbeat?
 
Upvote 0

lte5000

Member
Licensed User
Longtime User
Try changing the signature to
B4X:
b4j_ws.onclose = function(event)
{....}
It works for me. Also, are you implementing a heartbeat?

Just to clarify, b4j_ws.onclose does run when I stop the server, etc.

It just doesn't run when the problem (described above) occurs on Firefox/Safari.

As for a heartbeat, I call this function every five minutes, to keep the connection alive:
B4X:
Public Sub KeepAlive()
   Dim ft1 As Future = cb1.GetVal
   ws.Flush
End Sub

Should I be doing something differently here, or running it more often?

Thanks!
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
If you're running your heartbeat every 5 minutes and the connection dies at around 10 minutes, I suspect it's probably not a heartbeat issue. I tried to replicate the issue with Firefox and wasn't able to. Have you used FireBug to try to pin down the issue in Firefox?
 
Last edited:
Upvote 0

lte5000

Member
Licensed User
Longtime User
If you're running your heartbeat every 5 minutes and the connection dies at around 10 minutes, I suspect it's probably not a heartbeat issue. I tried to replicate the issue with Firefox and wasn't able to. Have you used FireBug to try to pin down the issue in Firefox?

I changed the interval to 1 minute and it seems to have solved the problem.

Thanks for your help Roycefer!
 
Upvote 0

lte5000

Member
Licensed User
Longtime User
Apparently that was the case.

My b4j server log did not always show that they died in less than 5 minutes - sometimes it was 6 or more minutes till it showed the client had disconnected.
I do not know very much about websockets, but apparently there can be some delay between when the connection actually dies and when the server 'knows" that it has died.
 
Upvote 0
Top