0

I need to connect to MQTT server, get some messages, then reconnect (disconnect and connect again).

I'm using sgcWebSockets v4.2.1 and official connect code:

// 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;

Now I need to disconnect. How do I do it?

I've tried a number of ways, but they all seem to fail. None of them appears as "Client disconnected" on the MQTT server logs even:

// "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;

What is the proper way of disconnecting (to reconnect back again) from a server with sgcWebSockets MQTT? If that's troublesome, how do I properly disconnect and dispose of old connection to create one anew?

Sergii Gryshkevych
  • 4,029
  • 1
  • 25
  • 42
Kromster
  • 7,181
  • 7
  • 63
  • 111

1 Answers1

0

Not a real answer, but still a workaround:

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

Now fSGCClient can be recreated and connected anew.

Kromster
  • 7,181
  • 7
  • 63
  • 111