Helper Class Reference  1.3
HomeGenie - Automation Programs' Engine API
Public Member Functions | List of all members
MqttClientHelper Class Reference

MQTT client helper. Class instance accessor: MqttClient More...

Public Member Functions

MqttClientHelper Service (string server)
 Sets the MQTT server to use. More...
 
MqttClientHelper Connect (string clientId)
 Connects to the MQTT server using the default port (1883) and the specified client identifier. More...
 
MqttClientHelper Connect (int port, string clientId)
 Connects to the MQTT server using the specified port and client identifier. More...
 
MqttClientHelper Connect (int port, string clientId, Action< bool > callback=null)
 Connects to the MQTT server using the specified port and client identifier. More...
 
MqttClientHelper Connect (int port, string clientId, Action< MqttClientOptionsBuilder > clientOptionsCallback, Action< bool > callback=null)
 Connects to the MQTT server using the specified port / client identifier and the specified client options. More...
 
MqttClientHelper Disconnect ()
 Disconnects from the MQTT server. More...
 
MqttClientHelper Subscribe (string topic, Action< string, byte[]> callback)
 Subscribe the specified topic. More...
 
MqttClientHelper Unsubscribe (string topic)
 Unsubscribe the specified topic. More...
 
MqttClientHelper Publish (string topic, string message)
 Publish a message to the specified topic. More...
 
MqttClientHelper Publish (string topic, byte[] message)
 Publish a message to the specified topic. More...
 
MqttClientHelper Publish (MqttApplicationMessage applicationMessage)
 Publish a message using advanced options. More...
 
MqttClientHelper UsingWebSockets (bool useWebSocket)
 Connect over WebSocket (default = false). More...
 
MqttClientHelper WithCredentials (string user, string pass)
 Use provided credentials when connecting. More...
 
MqttClientHelper WithTls (bool useTls)
 Set whether to connect using TLS/SSL or not. More...
 

Detailed Description

MQTT client helper. Class instance accessor: MqttClient

Member Function Documentation

◆ Service()

MqttClientHelper Service ( string  server)

Sets the MQTT server to use.

Parameters
serverMQTT server address.

◆ Connect() [1/4]

MqttClientHelper Connect ( string  clientId)

Connects to the MQTT server using the default port (1883) and the specified client identifier.

Parameters
clientIdThe client identifier.

◆ Connect() [2/4]

MqttClientHelper Connect ( int  port,
string  clientId 
)

Connects to the MQTT server using the specified port and client identifier.

Parameters
portMQTT server port.
clientIdThe client identifier.

◆ Connect() [3/4]

MqttClientHelper Connect ( int  port,
string  clientId,
Action< bool >  callback = null 
)

Connects to the MQTT server using the specified port and client identifier.

Parameters
portMQTT server port.
clientIdThe client identifier.
callbackOptional callback Action<bool> invoked when the connection status changed (the argument value will be true if connected, false otherwise)

MqttClient .Service(server) .UsingWebSockets(useWebSockets) .Connect(port, clientid, (connected) => { MqttIsConnected = connected; if (connected) { Program.Notify("Connected!"); } else { Program.Notify("Disconnected!"); } });

◆ Connect() [4/4]

MqttClientHelper Connect ( int  port,
string  clientId,
Action< MqttClientOptionsBuilder >  clientOptionsCallback,
Action< bool >  callback = null 
)

Connects to the MQTT server using the specified port / client identifier and the specified client options.

Parameters
portMQTT server port.
clientIdThe client identifier.
clientOptionsCallbackCallback Action<MqttClientOptionsBuilder> invoked before the connection is established to allow setting advanced connection options. See https://github.com/chkr1011/MQTTnet/wiki/Client for all available options.
callbackOptional callback Action<bool> invoked when the connection status changed (the argument value will be true if connected, false otherwise)

Example:

MqttClient
.Service(server)
.UsingWebSockets(useWebSockets)
.Connect(port, clientid, (options) => {
options.WithTls(new MqttClientOptionsBuilderTlsParameters {
UseTls = true,
CertificateValidationCallback = (X509Certificate x, X509Chain y, SslPolicyErrors z, IMqttClientOptions o) =>
{
// TODO: Check conditions of certificate by using above parameters.
return true;
}
});
}, (connected) => {
MqttIsConnected = connected;
if (connected) {
Program.Notify("Connected!");
} else {
Program.Notify("Disconnected!");
}
});

◆ Disconnect()

MqttClientHelper Disconnect ( )

Disconnects from the MQTT server.

◆ Subscribe()

MqttClientHelper Subscribe ( string  topic,
Action< string, byte[]>  callback 
)

Subscribe the specified topic.

Parameters
topicTopic name.
callbackCallback for receiving the subscribed topic messages.

◆ Unsubscribe()

MqttClientHelper Unsubscribe ( string  topic)

Unsubscribe the specified topic.

Parameters
topicTopic name.

◆ Publish() [1/3]

MqttClientHelper Publish ( string  topic,
string  message 
)

Publish a message to the specified topic.

Parameters
topicTopic name.
messageMessage text.

◆ Publish() [2/3]

MqttClientHelper Publish ( string  topic,
byte[]  message 
)

Publish a message to the specified topic.

Parameters
topicTopic name.
messageMessage text as byte array.

◆ Publish() [3/3]

MqttClientHelper Publish ( MqttApplicationMessage  applicationMessage)

Publish a message using advanced options.

Parameters
applicationMessageMqttApplicationMessage instance (See https://github.com/chkr1011/MQTTnet/wiki/Client#publishing-messages for documentation).

◆ UsingWebSockets()

MqttClientHelper UsingWebSockets ( bool  useWebSocket)

Connect over WebSocket (default = false).

Returns
NetHelper.
Parameters
useWebSockettrue/false

◆ WithCredentials()

MqttClientHelper WithCredentials ( string  user,
string  pass 
)

Use provided credentials when connecting.

Returns
NetHelper.
Parameters
userUsername.
passPassword.

◆ WithTls()

MqttClientHelper WithTls ( bool  useTls)

Set whether to connect using TLS/SSL or not.

Parameters
useTlsUse TLS/SSL.
Returns

The documentation for this class was generated from the following file: