#1 2021-11-28 23:59:04

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Immediate WebSocketsClosed event

How to set the websockets retry settings to prevent from raising the OnWebSocketsClosed event immediately after I kill the server.

That'my my code:

  with connector do
  begin
    KeepAliveMS := 1000 * 60 * 5; //5 mins
    Compression := [hcDeflate];
    Settings.HeartbeatDelay := 3000;
    Settings.DisconnectAfterInvalidHeartbeatCount := 5;
    Settings.ClientAutoUpgrade := True;
    OnWebSocketsUpgraded := WebSocketsUpgraded;
    OnWebSocketsClosed := WebSocketsClosed;
    ServiceDefine([IService], sicShared);
    if not connector.Services.Resolve(IService, serviceIntf) then
      raise EServiceException.Create('Service unavailable');
  end;

and seems that mORMot does not respects those settings.

Offline

#2 2021-11-29 07:47:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Immediate WebSocketsClosed event

I don't understand what you expect, and what you want to achieve, sorry.

Offline

#3 2021-11-29 12:26:27

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: Immediate WebSocketsClosed event

I thought that websocket will raise on OnWebSocketsClosed event after 5 retries (DisconnectAfterInvalidHeartbeatCount) x HeartbeatDelay (3000) = 5 times x 3 seconds = 15 seconds.

Offline

#4 2021-11-30 10:57:40

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Immediate WebSocketsClosed event

If you kill the server, then the connection is closed, therefore the OnWebSocketsClosed is notified before any hearbeat is sent.

The WebSockets protocol has a "focConnectionClose" frame which is to notify the other end of graceful reconnection.

So raising OnWebSocketsClosed immediately on graceful disconnection is what is expected IMHO - it is called "on websockets closed" as its name states.
Heartbeats are just a way of checking on an idle connection - not the only way of checking the connection state.

Offline

#5 2021-12-01 08:38:40

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: Immediate WebSocketsClosed event

So what is preferred method for websocket reconnection?

procedure WebSocketsClosed(  );
begin
  timerReconnect.Enabled := True;
end;

procedure WebSocketUpgraded(  );
begin
  serviceIntf.RegisterListener('xxxx', ServiceNotification);
end;  

procedure Reconnect;
begin
  if assigned(connector) then
   connector.Free;  
  ServiceNotification := nil;
  serviceIntf := nil;

  connector := TRestHttpClientWebsockets.Create('127.0.0.1', '1414', TOrmModel.Create([]), False, '', '', 0, 0, 3000);
  ServiceNotification := TServiceNotification.Create(connector, IServiceNotification);
  with connector do
  begin
    Settings.ClientAutoUpgrade := True;
    OnWebSocketsUpgraded := WebSocketsUpgraded;
    OnWebSocketsClosed := WebSocketsClosed;
    ServiceDefine([IService], sicShared);
    connector.Services.Resolve(IService, serviceIntf);
  end;
  connector.WebSocketsUpgrade('aaa');
  timerReconnect.Enabled := false;
end;

procedure timerReconnect(Sender: TObject);
begin
  try
    Reconnect;
  except
  end; 
end;

is this correct way ?

Offline

#6 2021-12-01 09:13:02

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Immediate WebSocketsClosed event

It may work.

But I am currently implementing automatic re-connection with full callbacks support at framework level.

Offline

#7 2021-12-01 10:53:13

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: Immediate WebSocketsClosed event

@AB fantastic, zero extra code for reconnecting feature! Thank you. When do you plan to implement reconnection?

Offline

Board footer

Powered by FluxBB