/// <summary> /// Subscribe the specified topic. /// </summary> /// <param name="topic">Topic.</param> public void Subscribe(string topic) { if (IsAvailable && !_subscribedTopics.Contains(topic)) { if (_client.IsConnected) { var topicID = _client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); Console.WriteLine("Client {0} subscribed to topic {1}({2})", _client.ClientId, topic, topicID); } _subscribedTopics.Add(topic); } }
private static void _subscribe(MqttConfig cfg) { //System.Threading.Timer tmr; //"192.168.9.21" var client = new uPLibrary.Networking.M2Mqtt.MqttClient(cfg.server); client.ProtocolVersion = uPLibrary.Networking.M2Mqtt.MqttProtocolVersion.Version_3_1_1; client.MqttMsgSubscribed += Client_MqttMsgSubscribed; client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived; client.MqttMsgPublished += Client_MqttMsgPublished; if (!string.IsNullOrWhiteSpace(cfg.username)) { client.Connect(Guid.NewGuid().ToString(), cfg.username, cfg.password); } else { client.Connect(Guid.NewGuid().ToString()); } var channels = new List <string>(); var qoss = new List <byte>(); { foreach (var s in cfg.subscriptions) { channels.Add(s.channel); qoss.Add(s.qos); } } /*client.Subscribe(new string[] {"house/serverroom/temp"}, new byte[] { 0});*/ client.Subscribe(channels.ToArray(), qoss.ToArray()); Console.WriteLine("Type 'q' to quit"); string text = null; do { text = Console.ReadLine(); } while(text != "q"); client.Disconnect(); }
public MqttClient(Connect.Conference.Core.Models.Conferences.Conference conference) { Conference = conference; if (!string.IsNullOrEmpty(conference.MqttBroker)) { var filename = string.Format("{0}\\Portals\\_default\\Logs\\Connect.Conference.{1}.{2:yyyy-MM-dd}", DotNetNuke.Common.Globals.ApplicationMapPath, Conference.ConferenceId, System.DateTime.Now); var i = 0; while (true) { try { if (Logger == null) { Logger = new StreamWriter(string.Format("{0}-{1}.resources", filename, i), true, Encoding.UTF8); } break; } catch (System.Exception) { } i++; } string clientId = string.Format("Connect.Conference.{0}.{1}", DotNetNuke.Entities.Host.Host.GUID, conference.ConferenceId); Client = new uPLibrary.Networking.M2Mqtt.MqttClient(conference.MqttBroker); Client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; if (string.IsNullOrEmpty(conference.MqttBrokerUsername)) { Client.Connect(clientId); } else { Client.Connect(clientId, conference.MqttBrokerUsername, conference.MqttBrokerPassword, false, 1000); } Client.Subscribe(new string[] { Conference.BaseTopicPath + "rfid/room/+" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); } }