private static void DoThreadAction(object nameObj) { string name = nameObj.ToString(); using (var c = new MqttClient(name)) { c.OnUnsolicitedMessage += (c_OnUnsolicitedMessage); IPAddress address = Dns.GetHostAddresses(Server) .First(a => a.AddressFamily == AddressFamily.InterNetwork); var test = new IPEndPoint(address, Port); Console.WriteLine("{0} connecting...", name); DemandWorked(c.Connect(test)); Console.WriteLine("{0} connected...", name); Console.WriteLine("{0} subscribing...", name); DemandWorked(c.Subscribe( new[] { new Subscription(topic, QualityOfService.AtMostOnce), }, null)); Console.WriteLine("{0} subscribed...", name); waitForSubscribed.Set(); done.WaitOne(-1); c.Disconnect(TimeSpan.FromSeconds(5)); } }
static void Main() { Console.CancelKeyPress += Console_CancelKeyPress; // const string server = "localhost"; // const string server = "broker.mqttdashboard.com"; const string server = "test.mosquitto.org"; const string clientName = "horvick"; using (var client = new MqttClient(clientName)) { client.OnUnsolicitedMessage += client_OnUnsolicitedMessage; client.OnNetworkDisconnected += client_OnNetworkDisconnected; client.Connect(server, cleanSession: true).Await(); client.Subscribe("#", QualityOfService.AtMostOnce).Await(); stopEvent.WaitOne(-1); } }