我需要连接到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的服务器断开连接(重新连接)的正确方法是什么?如果这很麻烦,我如何正确地断开连接并处理旧连接以重新创建一个连接?
发布于 2019-03-01 16:32:03
不是一个真正的答案,但仍然是一个变通方法:
if fSGCClient <> nil then
fSGCClient.Active := False;
FreeAndNil(fSGCClient);现在可以重新创建fSGCClient并重新连接。
https://stackoverflow.com/questions/54740826
复制相似问题