From ee00b6d82e6c40796579f06ac2012b4a02d38844 Mon Sep 17 00:00:00 2001 From: mateusvicente100 Date: Thu, 5 Nov 2020 15:27:08 -0300 Subject: [PATCH] Bug fixes and read timeout - One single message to send when try connect. - 30 seconds to read timeout when try connect. --- src/Bird.Socket.Client.pas | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Bird.Socket.Client.pas b/src/Bird.Socket.Client.pas index 378fe25..702fdae 100644 --- a/src/Bird.Socket.Client.pas +++ b/src/Bird.Socket.Client.pas @@ -144,6 +144,7 @@ procedure TBirdSocketClient.Connect; var LURI: TIdURI; LSecure: Boolean; + LContent: TStringList; begin if Connected then raise Exception.Create('The websocket is already connected!'); @@ -177,14 +178,20 @@ procedure TBirdSocketClient.Connect; inherited Connect; if not LURI.Port.IsEmpty then LURI.Host := LURI.Host + ':' + LURI.Port; - FSocket.WriteLn(Format('GET %s HTTP/1.1', [LURI.Path + LURI.Document])); - FSocket.WriteLn(Format('Host: %s', [LURI.Host])); - FSocket.WriteLn('User-Agent: Delphi WebSocket Simple Client'); - FSocket.WriteLn('Connection: keep-alive, Upgrade'); - FSocket.WriteLn('Upgrade: WebSocket'); - FSocket.WriteLn('Sec-WebSocket-Version: 13'); - FSocket.WriteLn(Format('Sec-WebSocket-Key: %s', [GenerateWebSocketKey])); - FSocket.WriteLn(EmptyStr); + LContent := TStringList.Create; + try + LContent.Add(Format('GET %s HTTP/1.1 ', [LURI.Path + LURI.Document])); + LContent.Add(Format('Host: %s ', [LURI.Host])); + LContent.Add('User-Agent: Delphi WebSocket Simple Client'); + LContent.Add('Connection: keep-alive, Upgrade'); + LContent.Add('Upgrade: WebSocket'); + LContent.Add('Sec-WebSocket-Version: 13'); + LContent.Add(Format('Sec-WebSocket-Key: %s ', [GenerateWebSocketKey])); + LContent.Add(EmptyStr); + FSocket.WriteLn(LContent.Text); + finally + LContent.Free; + end; ReadFromWebSocket; StartHeartBeat; finally @@ -323,6 +330,8 @@ function TBirdSocketClient.IsValidHeaders(const AHeaders: TStrings): Boolean; end; function TBirdSocketClient.IsValidWebSocket: Boolean; +const + READ_TIMEOUT = 30000; var LSpool: string; LByte: Byte; @@ -333,6 +342,7 @@ function TBirdSocketClient.IsValidWebSocket: Boolean; try try FUpgraded := False; + FSocket.ReadTimeout := READ_TIMEOUT; while Connected and not FUpgraded do begin LByte := FSocket.ReadByte;