Demos | Client MQTT

This demo shows how connect to a MQTT broker server. Requires a TsgcWebSocketClient to handle WebSocket / TCP protocols.

 

Configuration

 


if (mqtt == null)
{
	mqtt = new TsgcWSPClient_MQTT();
	mqtt.OnMQTTBeforeConnect += OnMQTTBeforeConnectEvent;
	mqtt.OnMQTTConnect += OnMQTTConnectEvent;
	mqtt.OnMQTTDisconnect += OnMQTTDisconnectEvent;
	mqtt.OnMQTTSubscribe += OnMQTTSubscribeEvent;
	mqtt.OnMQTTUnSubscribe += OnMQTTUnSubscribeEvent;
	mqtt.OnMQTTPing += OnMQTTPingEvent;
	mqtt.OnMQTTPubAck += OnMQTTPubAckEvent;
	mqtt.OnMQTTPubComp += OnMQTTPubCompEvent;
	mqtt.OnMQTTPublish += OnMQTTPublishEvent;
	mqtt.OnMQTTPubRec += OnMQTTPubRecEvent;
	mqtt.Client = client;
}
mqtt.Client = client;
txtParameters.Text = "/";
chkTLS.Checked = false;
mqtt.Authentication.Enabled = false;
mqtt.Authentication.UserName = "";
mqtt.Authentication.Password = "";
mqtt.MQTTVersion = TwsMQTTVersion.mqtt311;
mqtt.HeartBeat.Interval = 5;
mqtt.HeartBeat.Enabled = true;
switch (Index)
{
	case 0: // esegece.com      
		txtHost.Text = "www.esegece.com";
		txtPort.Text = "15675";
		txtParameters.Text = "/ws";
		mqtt.Authentication.Enabled = true;
		mqtt.Authentication.UserName = "sgc";
		mqtt.Authentication.Password = "sgc";
		chkOverWebSocket.Checked = true;
		break;
	case 1: // test.mosquitto.org      
		txtHost.Text = "test.mosquitto.org";
		txtPort.Text = "1883";
		chkTLS.Checked = false;
		chkOverWebSocket.Checked = false;
		break;
	case 2: // mqtt.fluux.io
		txtHost.Text = "mqtt.fluux.io";
		txtPort.Text = "1883";
		chkTLS.Checked = false;
		chkOverWebSocket.Checked = false;
		mqtt.MQTTVersion = TwsMQTTVersion.mqtt5;
		break;
	case 3: // broker.hivemq.com
		txtHost.Text = "broker.mqttdashboard.com";
		txtPort.Text = "8000";
		txtParameters.Text = "/mqtt";
		chkTLS.Checked = false;
		chkOverWebSocket.Checked = true;
		mqtt.MQTTVersion = TwsMQTTVersion.mqtt5;
		break;
}

MQTT Events

The connection flow is controlled by MQTT Client component, so you must handle the MQTT events to know when it's connected to broker, when a new message is published, when is disconnected...

 


private void OnMQTTConnectEvent(TsgcWSConnection Connection, bool Session, int ReasonCode, string ReasonName, TsgcWSMQTTCONNACKProperties ConnectProperties)
{
	DoLog("#MQTT Connect");
	chkTLS.Enabled = false;
	chkCompressed.Enabled = false;
	if (FMQTTSubscribeTopic != "")
	{
		mqtt.Subscribe(FMQTTSubscribeTopic);
		FMQTTSubscribeTopic = "";
	}
}

private void OnMQTTPublishEvent(TsgcWSConnection Connection, string Topic, string Text, TsgcWSMQTTPublishProperties PublishProperties) { DoLog(Topic + ": " + Text); }
private void OnMQTTSubscribeEvent(TsgcWSConnection Connection, int PacketIdentifier, TsgcWSSUBACKS Codes, TsgcWSMQTTSUBACKProperties SubscribeProperties) { DoLog("#Subscribe: " + PacketIdentifier.ToString()); }
private void OnMQTTDisconnectEvent(TsgcWSConnection Connection, int ReasonCode, string ReasonName, TsgcWSMQTTDISCONNECTProperties DisconnectProperties) { DoLog("#disconnected"); chkTLS.Enabled = true; chkCompressed.Enabled = true; }