Android Question websockets timeout

wl

Well-Known Member
Licensed User
Hi,

I noticed websockets can detected whether the connection was closed.
It *seems* this happens after a timeout period of around 30 seconds after the last messages that was tried being sent ?

So If I send a keep-alive message each 10 minutes and would disconnect immediately after such a keep-alive message, it would take another 10,5 minutes for the Closed method to be called ?

Is this correct ?

Thank you
 

Erel

Administrator
Staff member
Licensed User

LucaMs

Expert
Licensed User
I have tried in this way, in the "websocket (B4J) class":
B4X:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    ws.Session.MaxInactiveInterval = 10 ' seconds
End Sub
upload_2016-5-3_12-20-16.png

So, I expect that the event WebSocket_Disconnected would be triggered after 10 seconds of client inactivity but it wasn't, the connection remains active.

That should be the ideal solution. Otherwise, I thought about using a flag in the "websocket (B4J) class" and test it every X period using a timer, but I have to set this flag for each transaction made by the client and this is a bore (and I might forget to set it).


P.S. and I have to search if many timers (one of each connection) degrade server performance.
 
Last edited:

LucaMs

Expert
Licensed User
I have tried in this way, in the "websocket (B4J) class":
B4X:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    ws.Session.MaxInactiveInterval = 10 ' seconds
End Sub
View attachment 43738
So, I expect that the event WebSocket_Disconnected would be triggered after 10 seconds of client inactivity but it wasn't, the connection remains active.

That should be the ideal solution. Otherwise, I thought about using a flag in the "websocket (B4J) class" and test it every X period using a timer, but I have to set this flag for each transaction made by the client and this is a bore (and I might forget to set it).


P.S. and I have to search if many timers (one of each connection) degrade server performance.

This is because "probably" ( :)) I have confused "session" with "connection" ("... the SESSION will be invalidated...").
Indeed, in that my test the session is no longer valid after 10 seconds.

So, I suppose there is not a way, by setting something, to disconnect a websocket automatically after x minutes of inactivity, right? Should I use timers as I wrote in the previous post?
 
Last edited:

LucaMs

Expert
Licensed User
Probably (very probably :D) I'm wrong but...

I don't know how exactly B4J "creates" a websocket class (internally) but this class provides a websocket and two websocket events, WebSocket_Connected and WebSocket_Disconnected, which correspond to Jetty websocket events.

This document says that there is another event available, On Message; if this event was also available in the "B4J Websocket Class", I could set the flag I mentioned in #6 within the routine that handles this event, avoiding the problems I have mentioned there.

This post is "just to say", because, if it is possible, if you can add this event, I will have to wait for the new version of B4J :(


Thank you anyway for your previous answers, Erel :)
 
Top