Skip to content

Commit

Permalink
Bug fixes and read timeout
Browse files Browse the repository at this point in the history
- One single message to send when try connect.
- 30 seconds to read timeout when try connect.
  • Loading branch information
mateusvicente100 committed Nov 5, 2020
1 parent 079ea8a commit ee00b6d
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/Bird.Socket.Client.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit ee00b6d

Please sign in to comment.