Catching client exceptions

I'm making some tests with the socket client component to catch an solve network and connections exceptions.
The test i'm doing now is closing the socket server application suddenly and see the socket client behavior, but the client start a loop with this exception:

web socket client loop exception

Even if i reopen the server application, the client doesn't connect again with the server.
Any idea how i deal with this exception and force a new connection with the server?

Hi,

Can you provide a minimal example for this (both server and client)? It's hard to tell what is going on without any context. Just by connecting to the server then closing the server does not raise this exception.

Sure. Here's the socket server configuration:

FSocketServer := TTMSFNCWebSocketServer.Create;
FSocketServer.Port := 6963;
FSocketServer.PathName := '';
FSocketServer.OnMessageReceived := MessageReceived;

if Assigned(AEventConnected) then
  FSocketServer.OnConnect := AEventConnected;

if Assigned(AEventDisconnected) then
 FSocketServer.OnDisconnect := AEventDisconnected;

FSocketServer.OnMessageReceived := MessageReceived;
FSocketServer.OnHandshakeResponseSent := HandshakeResponseSent;
FSocketServer.OnPing := SocketServerPing;
FSocketServer.Active := True;

now, the socket client configuration:

FSocketClient := TTMSFNCWebsocketClient.Create(nil);
FSocketClient.HostName := 'localhost';
FSocketClient.Port := 6963;
FSocketClient.PathName := '';
FSocketClient.UseSSL := false;
FSocketClient.OnMessageReceived := MessageReceived;

FSocketClient.Connect;

note that both socket server and client are created dynamically. This is the only configuration i do on the components i used to have a TTimer for make the Ping calls but i disable it for a while during the tests.

Are there any messages sent between the server and the client? When and how?

No. i send no messages in the test, only connect the client with the server, wait some seconds and then close the server application. After this the exception is raised.

Actually, one handshake message is sent by the server to the client when i connect, the message is a json:

{"handshake_message":"Server operating"}

i'm not using this handshake message now, the client "OnMessageReceived" is assigned with a "application.processmessages" command, this way:

procedure TModelWEBSocket.SocketMessageReceived(AMessage: string);
begin
  Application.ProcessMessages;
end;

Are you sure that is all? With the given information we are only able to reproduce this if the server instance is not destroyed when the application stops. Given you are creating this dynamically, where are you calling FSocketServer.Free?

Yes, i'm calling the .Free on the instance, but i was calling on another place, not when the application stops, i changed the place that was calling the .Free for the OnDestroy of the form:

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  if Assigned(FSocketServer) then
    FreeAndNil(FSocketServer);
end;

doing this way, no exception on the client. i'll continue making the tests on the component and start a new connection. Thanks for the help