#1 2025-04-20 05:59:03

anouri
Member
Registered: 2024-02-11
Posts: 93

Slow connection to TWebSocketServer

I created TWebSocketServer server and TWebSocketProtocolChat. When I connect it with JS it is fast.
But when I connect with below code it takes 5 second to upgrade.

var
  ClientWS      : THttpClientWebSockets;
  lProto        : TWebSocketProtocolEcho;
begin
  lProto := TWebSocketProtocolEcho.Create('meow','');
  lProto.OnIncomingFrame := EchoFrame;
  ClientWS := THttpClientWebSockets.Create;
  ClientWS.Open('127.0.0.1', '8383');
  ClientWS.WebSocketsUpgrade('', '', false, [], lProto, '');

Offline

#2 2025-04-20 06:12:33

anouri
Member
Registered: 2024-02-11
Posts: 93

Re: Slow connection to TWebSocketServer

I accidentally changed the position of a line and the problem was solved. But why should this happen?

var
  ClientWS      : THttpClientWebSockets;
  lProto        : TWebSocketProtocolEcho;
begin
  lProto := TWebSocketProtocolEcho.Create('meow','');
  ClientWS := THttpClientWebSockets.Create;
  ClientWS.Open('127.0.0.1', '12346');
  ClientWS.WebSocketsUpgrade('', '', false, [], lProto, '');
  lProto.OnIncomingFrame := EchoFrame; //this line moved here

Last edited by anouri (2025-04-20 06:12:46)

Offline

#3 2025-04-20 06:54:19

anouri
Member
Registered: 2024-02-11
Posts: 93

Re: Slow connection to TWebSocketServer

Is this bug or abnormal behaviour?

Last edited by anouri (2025-04-20 06:55:06)

Offline

#4 Yesterday 08:05:31

anouri
Member
Registered: 2024-02-11
Posts: 93

Re: Slow connection to TWebSocketServer

I think there is issue in somewhere in mormot.
When lProto.OnIncomingFrame assigned before socket upgrade this function takes 5 seconds to finish

procedure TWebSocketProcess.WaitThreadStarted;
var
  endtix: Int64;
begin
  endtix := GetTickCount64 + 5000;
  repeat
    SleepHiRes(0);
  until fProcessEnded or
        (fState <> wpsCreate) or
        (GetTickCount64 > endtix);
end;

Offline

#5 Yesterday 09:50:59

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

Re: Slow connection to TWebSocketServer

What do you do in your EchoFrame method?
Perhaps this is blocking the process.

You should ignore the first call with frame.opcode=focContinuation in your callback.

Offline

#6 Today 18:04:33

anouri
Member
Registered: 2024-02-11
Posts: 93

Re: Slow connection to TWebSocketServer

EchoFrame method:

procedure TForm2.EchoFrame(Sender: TWebSocketProcess; const Frame: WebSocketFrame);
begin
  case Frame.opcode of
    focContinuation:
    begin
      TThread.Synchronize(nil, procedure
      begin
        Memo1.Lines.Add('connected');
      end);
    end;
  end;
end;

Offline

#7 Today 18:09:11

anouri
Member
Registered: 2024-02-11
Posts: 93

Re: Slow connection to TWebSocketServer

TThread.Queue Solved the issue!

Offline

Board footer

Powered by FluxBB