使用SGC WebSockets时如何断开与MQTT服务器的连接?

问题描述 投票:0回答:1

我需要连接到MQTT服务器,获取一些消息,然后重新连接(断开连接并再次连接)。

我正在使用sgcWebSockets v4.2.1和官方连接代码:

// Create websocket client and set server options
oClient := TsgcWebSocketClient.Create(nil);
oClient.Host := 'wss://myserver';
oClient.Port := 443;

// Create MQTT protocol and assign to websocket client
oMQTT := TsgcWSPClient_MQTT.Create(nil);
oMQTT.Client := oClient;

// MQTT Authentication
oMQTT.Authentication.Enabled := True;
oMQTT.Authentication.Username := 'user';
oMQTT.Authentication.Password := 'pass';

// Handle MQTT methods
oMQTT.OnMQTTConnect := OnMQTTConnectHandler;
oMQTT.OnMQTTDisconnect := OnMQTTDisconnectHandler;

// connect to server
oClient.Active := True;

现在我需要断开连接。我该怎么做?

我尝试了很多方法,但它们似乎都失败了。它们都不会在MQTT服务器日志上显示为“客户端已断开连接”,甚至:

// "RefusedUserPassword" on connecting again
// (since server never got the Disconnect message?)
//oClient.Active := False;

// Invalid control OptCode crash
//oMQTT.Disconnect;
//oClient.Active := False;

// RefusedUserPassword on connecting again
// (since server never got the Disconnect message?)
//oClient.Active := False;
//oMQTT.Disconnect;

// Odd error or no connection
//oMQTT.Disconnect;

使用sgcWebSockets MQTT从服务器断开连接(重新连接)的正确方法是什么?如果这很麻烦,我如何正确断开和处理旧连接以重新创建一个?

delphi mqtt delphi-10.3-rio sgc-websockets
1个回答
0
投票

不是一个真正的答案,但仍然是一个解决方法:

if fSGCClient <> nil then
  fSGCClient.Active := False;
FreeAndNil(fSGCClient);

现在fSGCClient可以重新创建并重新连接。

© www.soinside.com 2019 - 2024. All rights reserved.