Class MQTTClient
- Namespace
- VisioForge.Plugins.MQTT
- Assembly
- VisioForge.Plugins.MQTT.dll
Provides MQTT client functionality for publishing and subscribing to MQTT broker messages.
public class MQTTClient : IMQTTClient, IDisposableInheritance
Implements
Inherited Members
Remarks
This class wraps the MQTTnet library to provide simplified MQTT client operations including connection management, message publishing with various payload types, and topic subscription management. Supports asynchronous operations and proper resource cleanup through IDisposable pattern.
Methods
ConnectAsync(string, int, string, string, CancellationToken?)
Asynchronously connects to an MQTT broker server.
public Task<bool> ConnectAsync(string server, int port, string username = null, string password = null, CancellationToken? cancellationToken = null)Parameters
serverstring-
The hostname or IP address of the MQTT broker server.
portint-
The port number of the MQTT broker server.
usernamestring-
Optional username for authentication. Can be null if authentication is not required.
passwordstring-
Optional password for authentication. Can be null if authentication is not required.
cancellationTokenCancellationToken?-
Optional cancellation token to cancel the operation. If null, the operation cannot be cancelled.
Returns
- Task<bool>
-
A task that represents the asynchronous operation. The task result contains true if the connection was successful, otherwise false.
Remarks
This method creates a new MQTT client and establishes a connection to the specified broker. If both username and password are provided, they will be used for authentication. The connection uses TCP transport without TLS by default.
DisconnectAsync()
Asynchronously disconnects from the MQTT broker server.
public Task DisconnectAsync()Returns
- Task
-
A task that represents the asynchronous disconnect operation.
Remarks
This method gracefully disconnects from the MQTT broker. If the client is not connected, the method returns immediately.
Dispose(bool)
Releases the unmanaged resources used by the VisioForge.Plugins.MQTT.MQTTClient and optionally releases the managed resources.
protected virtual void Dispose(bool disposing)Parameters
disposingbool-
True to release both managed and unmanaged resources; false to release only unmanaged resources.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()Remarks
This method disposes the underlying MQTT client and releases all associated resources. After disposal, the client instance should not be reused.
~MQTTClient()
Finalizes an instance of the VisioForge.Plugins.MQTT.MQTTClient class.
protected ~MQTTClient()PublishAsync(string, string, CancellationToken?)
Asynchronously publishes a string message to the specified MQTT topic.
public Task PublishAsync(string topic, string payload, CancellationToken? cancellationToken = null)Parameters
topicstring-
The MQTT topic to publish the message to.
payloadstring-
The string payload to publish.
cancellationTokenCancellationToken?-
Optional cancellation token to cancel the operation. If null, the operation cannot be cancelled.
Returns
- Task
-
A task that represents the asynchronous publish operation.
Remarks
This method publishes a text message to the specified topic. If the client is not connected, the method returns immediately without publishing.
PublishAsync(string, MQTTJPEGFrame, CancellationToken?)
Asynchronously publishes a JPEG-encoded video frame to the specified MQTT topic.
public Task PublishAsync(string topic, MQTTJPEGFrame data, CancellationToken? cancellationToken = null)Parameters
topicstring-
The MQTT topic to publish the frame to.
dataMQTTJPEGFrame-
The VisioForge.Plugins.MQTT.MQTTJPEGFrame containing the JPEG-encoded image data to publish.
cancellationTokenCancellationToken?-
Optional cancellation token to cancel the operation. If null, the operation cannot be cancelled.
Returns
- Task
-
A task that represents the asynchronous publish operation.
Remarks
This method publishes binary JPEG frame data to the specified topic. If the client is not connected, the method returns immediately without publishing. The frame's timestamp is not included in the message payload but could be added as a user property if needed.
SubscribeAsync(string, CancellationToken?)
Asynchronously subscribes to the specified MQTT topic to receive messages.
public Task SubscribeAsync(string topic, CancellationToken? cancellationToken = null)Parameters
topicstring-
The MQTT topic to subscribe to. Supports wildcard patterns (+ for single level, # for multi-level).
cancellationTokenCancellationToken?-
Optional cancellation token to cancel the operation. If null, the operation cannot be cancelled.
Returns
- Task
-
A task that represents the asynchronous subscribe operation.
Remarks
This method subscribes to messages on the specified topic with QoS level "At Least Once" (QoS 1). If the client is not connected, the method returns immediately without subscribing.
UnsubscribeAsync(string, CancellationToken?)
Asynchronously unsubscribes from the specified MQTT topic.
public Task UnsubscribeAsync(string topic, CancellationToken? cancellationToken = null)Parameters
topicstring-
The MQTT topic to unsubscribe from.
cancellationTokenCancellationToken?-
Optional cancellation token to cancel the operation. If null, the operation cannot be cancelled.
Returns
- Task
-
A task that represents the asynchronous unsubscribe operation.
Remarks
This method stops receiving messages from the specified topic. If the client is not connected, the method returns immediately.