Last active
February 15, 2024 12:10
-
-
Save KeesCBakker/d8e093c237bf939004f830406cb35c09 to your computer and use it in GitHub Desktop.
Settings to connect to MQTT using M2Mqtt (can be used with CloudMqtt)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel.DataAnnotations; | |
using uPLibrary.Networking.M2Mqtt; | |
using uPLibrary.Networking.M2Mqtt.Messages; | |
var settings = AppSettingsProvider.Create<MqttSettings>(); | |
var port = Convert.ToInt32(settings.Port); | |
MqttClient client = null; | |
if (settings.UseSecureConnection) | |
{ | |
client = new MqttClient(settings.BrokerHostName, port, true, null, null, MqttSslProtocols.TLSv1_2); | |
} | |
else | |
{ | |
client = new MqttClient(settings.BrokerHostName, port, false, null, null, MqttSslProtocols.None); | |
} | |
client.Connect(Guid.NewGuid().ToString(), settings.UserName, settings.Password); | |
client.Subscribe(new string[] { settings.Topic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); | |
class MqttSettings | |
{ | |
[Required] | |
public string BrokerHostName { get; set; } | |
[Required] | |
public uint Port { get; set; } | |
[Required] | |
public bool UseSecureConnection { get; set; } | |
[Required] | |
public string UserName { get; set; } | |
[Required] | |
public string Password { get; set; } | |
[Required] | |
public string Topic { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh, I have the url. I was referring to running M2Mqtt on Unity.