TsgcWSPClient_MQTT | Client MQTT Connect

Bir MQTT Sunucusuna bağlanmak için önce bir TsgcWebSocketClient ve bir TsgcWSPClient_MQTT oluşturmalısınız. Ardından MQTT Bileşenini WebSocket İstemcisine eklemelisiniz.

 

Temel Kullanım

websocket protokolü kullanarak Mosquitto MQTT sunucusuna bağlanın. Bağlandıktan sonra "topic1" konusuna (topic) abone olun.

 


oClient := TsgcWebSocketClient.Create(nil);
oClient.Host := 'test.mosquitto.org';
oClient.Port := 8080;
oMQTT := TsgcWSPClient_MQTT.Create(nil);
oMQTT.Client := oClient;
oClient.Active := True;
 
procedure OnMQTTConnect(Connection: TsgcWSConnection; const Session: Boolean; const ReasonCode: Integer; 
  const ReasonName: string; const ConnectProperties: TsgcWSMQTTCONNACKProperties);
begin
  oMQTT.Subscribe('topic1');
end;

 

Client Identifier

MQTT, istemci bağlantısını tanımlamak için bir Client Identifier gerektirir. Bileşen otomatik olarak bir rastgele değer ayarlar ancak gerekirse kendi Client Identifier'ınızı ayarlayabilirsiniz, bunu yapmak için yalnızca OnBeforeConnect olayını işleyin ve değerinizi aClientIdentifier parametresinde ayarlayın.

 


procedure OnMQTTBeforeConnect(Connection: TsgcWSConnection; var aCleanSession: Boolean; 
  var aClientIdentifier: string);
begin
  aClientIdentifier := 'your client id';
end;

Authentication

Bazı sunucular, MQTT bağlantılarını yetkilendirmek için bir kullanıcı ve parola gerektirir. Sunucuya bağlanmadan önce kullanıcı adı ve parola değerini ayarlamak için Authentication özelliğini kullanın.

 


oMQTT := TsgcWSPClient_MQTT.Create(nil);
oMQTT.Authentication.Enabled := True;
oMQTT.Authentication.UserName := 'your user';
oMQTT.Authentication.Password := 'your password';