我需要连接到 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 从服务器断开(再次重新连接)的正确方法是什么?如果这很麻烦,我如何正确断开并处理旧连接以重新创建一个?