|
@@ -112,6 +112,7 @@ Type
|
|
FOnControl: TWSControlEvent;
|
|
FOnControl: TWSControlEvent;
|
|
FOnDisconnect: TNotifyEvent;
|
|
FOnDisconnect: TNotifyEvent;
|
|
FOnConnect: TNotifyEvent;
|
|
FOnConnect: TNotifyEvent;
|
|
|
|
+ procedure FreeConnectionObjects;
|
|
procedure SetActive(const Value: Boolean);
|
|
procedure SetActive(const Value: Boolean);
|
|
procedure SetHostName(const Value: String);
|
|
procedure SetHostName(const Value: String);
|
|
procedure SetMessagePump(AValue: TWSMessagePump);
|
|
procedure SetMessagePump(AValue: TWSMessagePump);
|
|
@@ -183,7 +184,7 @@ Type
|
|
Property OnHandshakeResponse : TWSClientHandshakeResponseEvent Read FOnHandshakeResponse Write FOnHandshakeResponse;
|
|
Property OnHandshakeResponse : TWSClientHandshakeResponseEvent Read FOnHandshakeResponse Write FOnHandshakeResponse;
|
|
// Called when a text message is received.
|
|
// Called when a text message is received.
|
|
property OnMessageReceived: TWSMessageEvent read FOnMessageReceived write FOnMessageReceived;
|
|
property OnMessageReceived: TWSMessageEvent read FOnMessageReceived write FOnMessageReceived;
|
|
- // Called when a connection is disconnected. Sed
|
|
|
|
|
|
+ // Called when a connection is disconnected. Sender is the TWebSocketClientConnection object.
|
|
property OnDisconnect: TNotifyEvent read FOnDisconnect write FOnDisconnect;
|
|
property OnDisconnect: TNotifyEvent read FOnDisconnect write FOnDisconnect;
|
|
// Called when a connection is established
|
|
// Called when a connection is established
|
|
property OnConnect: TNotifyEvent read FOnConnect write FOnConnect;
|
|
property OnConnect: TNotifyEvent read FOnConnect write FOnConnect;
|
|
@@ -270,15 +271,16 @@ begin
|
|
If Assigned(MessagePump) then
|
|
If Assigned(MessagePump) then
|
|
MessagePump.RemoveClient(FConnection);
|
|
MessagePump.RemoveClient(FConnection);
|
|
If Assigned(OnDisconnect) then
|
|
If Assigned(OnDisconnect) then
|
|
- OnDisconnect(FConnection);
|
|
|
|
|
|
+ OnDisconnect(Self);
|
|
|
|
+ // We cannot free the connection here, because it still needs to call it's own OnDisconnect.
|
|
end;
|
|
end;
|
|
|
|
|
|
procedure TCustomWebsocketClient.Connect;
|
|
procedure TCustomWebsocketClient.Connect;
|
|
begin
|
|
begin
|
|
If Active then
|
|
If Active then
|
|
Exit;
|
|
Exit;
|
|
- FreeAndNil(FSocket);
|
|
|
|
- FreeAndNil(FTransport);
|
|
|
|
|
|
+ // Safety: Free any dangling objects before recreating
|
|
|
|
+ FreeConnectionObjects;
|
|
FSocket:=TInetSocket.Create(HostName,Port,ConnectTimeout);
|
|
FSocket:=TInetSocket.Create(HostName,Port,ConnectTimeout);
|
|
FTransport:=TWSClientTransport.Create(FSocket);
|
|
FTransport:=TWSClientTransport.Create(FSocket);
|
|
FConnection:=CreateClientConnection(FTransport);
|
|
FConnection:=CreateClientConnection(FTransport);
|
|
@@ -303,6 +305,7 @@ begin
|
|
DisConnect(False);
|
|
DisConnect(False);
|
|
FreeAndNil(FHandShake);
|
|
FreeAndNil(FHandShake);
|
|
FreeAndNil(FHandshakeResponse);
|
|
FreeAndNil(FHandshakeResponse);
|
|
|
|
+ FreeConnectionObjects;
|
|
Inherited;
|
|
Inherited;
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -449,6 +452,15 @@ begin
|
|
FConnection.Send(ftPong,TEncoding.UTF8.GetAnsiBytes(aMessage));
|
|
FConnection.Send(ftPong,TEncoding.UTF8.GetAnsiBytes(aMessage));
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+procedure TCustomWebsocketClient.FreeConnectionObjects;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ FreeAndNil(FConnection);
|
|
|
|
+ FreeAndNil(FTransport);
|
|
|
|
+ FreeAndNil(FSocket);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+
|
|
procedure TCustomWebsocketClient.Disconnect(SendClose : boolean = true);
|
|
procedure TCustomWebsocketClient.Disconnect(SendClose : boolean = true);
|
|
|
|
|
|
begin
|
|
begin
|
|
@@ -458,11 +470,10 @@ begin
|
|
Connection.Close('');
|
|
Connection.Close('');
|
|
if Assigned(MessagePump) then
|
|
if Assigned(MessagePump) then
|
|
MessagePump.RemoveClient(Connection);
|
|
MessagePump.RemoveClient(Connection);
|
|
- FreeAndNil(FConnection);
|
|
|
|
- FActive:=False;
|
|
|
|
If Assigned(OnDisconnect) then
|
|
If Assigned(OnDisconnect) then
|
|
- OnDisconnect(FConnection);
|
|
|
|
- // ConnectionDisconnected(Self);
|
|
|
|
|
|
+ OnDisconnect(Self);
|
|
|
|
+ FreeConnectionObjects;
|
|
|
|
+ FActive:=False;
|
|
end;
|
|
end;
|
|
|
|
|
|
procedure TCustomWebsocketClient.SetActive(const Value: Boolean);
|
|
procedure TCustomWebsocketClient.SetActive(const Value: Boolean);
|