0 votes
by (120 points)

What does client.Options.KeepAliveInterval do? I'm trying to keep the connection open at all times (using .net framework 3.5). From past experience, in case of no activity the client (python websocket) would close the connection after 10 minutes but also in case there is activity the server (aws api gateway) would close the connection after 2h.

Running a test for 5 hours, with KeepAliveInterval set to 3h,

            new AutoResetEvent(false),
            (state, bTimeout) => Console.WriteLine("Is connected: {0}, state: {1}, time: {2}", client.IsConnected, client.State, DateTimeOffset.UtcNow.ToString("HH:mm:ss")),

and the connection is still open

Is connected: True, state: Open, time: 11:07:05

Does the KeepAliveInterval keep the connection alive for at least X time set by this property and then some re-connection logic needs to be implemented to keep the connection open or is there no guarantee for at least X time?

Also tried to test the re-connection like this but only closing part worked, the connecting part failed stating the connection is already opened even though the logs were reporting

Is connected: False, state: Closed, time: 11:08:05

            new AutoResetEvent(false),
            (state, bTimeout) => client.Close(),

            new AutoResetEvent(false),
            (state, bTimeout) => client.Connect(uri),

Do I need to dispose and reconstruct the client object in order to reconnect?

Applies to: Rebex WebSocket

1 Answer

0 votes
by (70.2k points)

How keep-alive logic works:

When the client is waiting for a data to be received (it means the client is waiting for the Receive() or Poll() methods to be completed) the keep-alive packets are sent over the connection to keep it alive. However, if you are not waiting for a data (it means there is no Receive() or Poll() method in the progress) the keep-alive packets are not sent.

To keep the connection alive, just call Receive() or Poll() methods steadily.

How KeepAliveInterval option works:

It ensures that a keep-alive packet (PING) is sent to the server after specified time interval (during Receive or Poll operations).

It does not reconnect the object, in case of failed connection.

Note: there is at max 10 seconds delay for sending the first keep-alive packet. So, for example, if you specify KeepAliveInterval to 120 seconds, the first keep-alive packet can be possibly sent up to after 130 seconds of inactivity.

How IsConnected and State properties works:

The IsConnected and State properties does not provide current connection state. Please see documentation:

Gets the last known state of the WebSocket object.

It means the properties can report IsConnected: true, State: Open even the connection is closed for hours in reality. These properties are based on the result of the last Send/Receive operation. The only reliable way to determine whether the connection is still alive is to use it (perform Send/Receive) and handle error on possible failure.

How Connect() method works:

I think you got the error "The WebSocket has already been connected."
Please note that it does not say that the object is connected. It says that the Connect() method was already called and our API does not allow to call the Connect() method again (the object has already reached its final Closed state).

It means yes, you need to dispose and reconstruct the client object in order to reconnect.