TsgcWSPClient_MQTT | Client MQTT Connect

MQTT Server에 연결하려면 먼저 TsgcWebSocketClientTsgcWSPClient_MQTT를 만들어야 합니다. 그런 다음 MQTT Component를 WebSocket Client에 연결해야 합니다.

 

기본 사용법

websocket 프로토콜을 사용하여 Mosquitto MQTT 서버에 연결합니다. 연결 후 topic: "topic1"을 구독합니다.

 


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는 클라이언트 연결을 식별하기 위해 Client Identifier가 필요합니다. 구성 요소는 자동으로 임의의 값을 설정하지만, 필요한 경우 직접 Client Identifier를 설정할 수 있습니다. 이렇게 하려면 OnBeforeConnect 이벤트를 처리하고 aClientIdentifier 매개변수에 값을 설정하기만 하면 됩니다.

 


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

Authentication

일부 서버는 MQTT 연결을 authorize하기 위해 사용자와 비밀번호를 요구합니다. 서버에 연결하기 전에 Authentication 속성을 사용하여 사용자 이름과 비밀번호 값을 설정하십시오.

 


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