I'm trying to use 2 MQTT broker in one program with C#.
I'm using 'M2MQTT.Net' library to use MQTT application in my program.
First of all, I succeed to make single MQTT connection with broker.
Also, succeed to subscribe & publish with topic.
// Simple code that i make single connection with broker.
private void InitMqtt()
{
MqttClient = new MqttClient(brokerAddr);
}
// MqttClinet() method in the M2MQTT.Net library, MQTTClient class.
public MqttClient(string brokerHostName)
: this(brokerHostName, 1883, secure: false, null, null, MqttSslProtocols.None)
{
}
So what if i want to make MQTT connection over than 1, is this right code?
private void InitMqtt()
{
MqttClient = new MqttClient(brokerAddr);
MqttClient2 = new MqttClient(brokerAddr2);
MqttClient3 = new MqttClient(brokerAddr3);
}
If it's possible to make multi-connection with various mqtt broker,
can i handle the events from 3 brokers individually?
Thanks for reading. Regards.
It's possible to connect multi Clients to Multi Brokers in a single task, but each Mqtt client has its own callbacks method. Somethings like this:
protected MyMqttClient(string name ,MqttClientOptions options, List<string> TopicFiletrs)
{
//this._name = $"{GetType().Name}({name})";
this._name = name;
var factory = new MqttFactory();
_client = factory.CreateMqttClient();
_client.ApplicationMessageReceivedAsync += _client_ApplicationMessageReceivedAsync;
_client.ConnectedAsync += _client_ConnectedAsync;
_client.DisconnectedAsync += _client_DisconnectedAsync;
_options = options;
_TopicFiletrs = TopicFiletrs;
}