private static async Task SubscriberTest() { Console.WriteLine($"{DateTime.Now:g} - Connecting Subscribers"); while (Mqtt.Clients.Count < SubscriberCount) { var id = Guid.NewGuid().ToString(); await Mqtt.Connect(id, new[] { $"d/{id}" }); } Console.WriteLine($"{DateTime.Now:g} - All Subscribers Connected \t\t\t\t Avg Connection Time: {Mqtt.ConnectionTimes.Average()} ms"); await Task.Delay(2500); }
private static async Task PublishFromApiTest() { Console.WriteLine($"{DateTime.Now:g} - Connecting API"); var client = await Mqtt.GetClient($"api-{Guid.NewGuid().ToString().Substring(0, 8)}", Mqtt.Clients.Select(x => "a/" + x.Key).ToArray()); var timer = new System.Timers.Timer { Interval = PublishingRateSec * 1000 }; timer.Elapsed += async(sender, args) => { await Mqtt.Publish(client, Mqtt.GetRandomTopic()); }; timer.Start(); }
private static void SubscriberTestSpinning() { Console.WriteLine($"{DateTime.Now:g} - Connecting Subscribers"); while (true) { if (ConnectedClients < SubscriberCount) { ConnectedClients++; var id = Guid.NewGuid().ToString(); var task = Mqtt.Connect(id, new[] { $"d/{id}" }); Task.Run(() => task); } else { Thread.Sleep(1000); } } }
private static void StartRandomPublisher() { var publisher = Mqtt.GetPublisher(); if (publisher == null) { return; } Console.WriteLine($"{DateTime.Now:g} - Starting Publisher {publisher.Value.Key}"); var timer = new System.Timers.Timer { Interval = PublishingRateSec * 1000 }; timer.Elapsed += async(sender, args) => { await Mqtt.Publish(publisher.Value.Value, $"a/{publisher.Value.Key}"); }; timer.Start(); }