public async Task Subscribe_Lots_In_Single_Request() { using (var testEnvironment = new TestEnvironment(TestContext)) { var receivedMessagesCount = 0; await testEnvironment.StartServer(); var c1 = await testEnvironment.ConnectClient(); c1.UseApplicationMessageReceivedHandler(c => Interlocked.Increment(ref receivedMessagesCount)); var optionsBuilder = new MqttClientSubscribeOptionsBuilder(); for (var i = 0; i < 500; i++) { optionsBuilder.WithTopicFilter(i.ToString(), MqttQualityOfServiceLevel.AtMostOnce); } await c1.SubscribeAsync(optionsBuilder.Build()).ConfigureAwait(false); var c2 = await testEnvironment.ConnectClient(); var messageBuilder = new MqttApplicationMessageBuilder(); for (var i = 0; i < 500; i++) { messageBuilder.WithTopic(i.ToString()); await c2.PublishAsync(messageBuilder.Build()).ConfigureAwait(false); } SpinWait.SpinUntil(() => receivedMessagesCount == 500, TimeSpan.FromSeconds(20)); Assert.AreEqual(500, receivedMessagesCount); } }
public async Task <Dictionary <string, Qos> > SubscribeAsync(Dictionary <string, Qos> subscriptions, CancellationToken cancellationToken) { var builder = new MqttClientSubscribeOptionsBuilder(); foreach (var entry in subscriptions) { builder.WithTopicFilter(entry.Key, MapQos(entry.Value)); } try { var subscribeResult = await client.SubscribeAsync(builder.Build(), cancellationToken); var gainedQoses = new Dictionary <string, Qos>(); foreach (var item in subscribeResult.Items) { var gained = (int)item.ResultCode; if (gained < 0 || gained > 2) { throw new MqttException(message: $"Subscribe [topiic={item.TopicFilter.Topic}, qos={item.TopicFilter.QualityOfServiceLevel} failed: [resultCode={item.ResultCode}]."); } gainedQoses[item.TopicFilter.Topic] = (Qos)gained; } return(gainedQoses); } catch (Exception e) { throw new MqttException(cause: e); } }
public MQTTClient() { var mqttClient = new MqttFactory().CreateMqttClient(); var optionBuilder = new MqttClientOptionsBuilder(); optionBuilder .WithClientId("c001") .WithTcpServer(opts => { opts.Server = "127.0.0.1"; opts.Port = 108; }) .WithCredentials("u001", "p001") .WithCleanSession(true); var options = optionBuilder.Build(); mqttClient.ConnectAsync(options, CancellationToken.None).Wait(); var recieveHander = new MsgRecieveHandler(); mqttClient.ApplicationMessageReceivedHandler = recieveHander; var topicBuilder = new TopicFilterBuilder(); topicBuilder.WithTopic("家/客厅/空调/#") .WithAtMostOnceQoS(); var topicHome = topicBuilder.Build(); var subscribOptionBuilder = new MqttClientSubscribeOptionsBuilder(); subscribOptionBuilder.WithTopicFilter(topicHome); var subOpt = subscribOptionBuilder.Build(); mqttClient.SubscribeAsync(subOpt, CancellationToken.None).Wait(); var appMsg = new MqttApplicationMessage(); appMsg.Topic = "家/客厅/空调/开关"; appMsg.Payload = Encoding.UTF8.GetBytes("我来了~"); appMsg.QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce; appMsg.Retain = false; mqttClient.PublishAsync(appMsg, CancellationToken.None).Wait(); //var appMsg = new MqttApplicationMessage("家/客厅/空调/开关", // Encoding.UTF8.GetBytes("消息内容"), // MqttQualityOfServiceLevel.AtMostOnce, false); }
public async Task Subscribe_Lots_In_Single_Request() { using (var testEnvironment = CreateTestEnvironment()) { var receivedMessagesCount = 0; await testEnvironment.StartServer(); var c1 = await testEnvironment.ConnectClient(); c1.ApplicationMessageReceivedAsync += e => { Interlocked.Increment(ref receivedMessagesCount); return(PlatformAbstractionLayer.CompletedTask); }; var optionsBuilder = new MqttClientSubscribeOptionsBuilder(); for (var i = 0; i < 500; i++) { optionsBuilder.WithTopicFilter(i.ToString()); } await c1.SubscribeAsync(optionsBuilder.Build()).ConfigureAwait(false); var c2 = await testEnvironment.ConnectClient(); var messageBuilder = new MqttApplicationMessageBuilder(); for (var i = 0; i < 500; i++) { messageBuilder.WithTopic(i.ToString()); await c2.PublishAsync(messageBuilder.Build()).ConfigureAwait(false); } SpinWait.SpinUntil(() => receivedMessagesCount == 500, TimeSpan.FromSeconds(20)); Assert.AreEqual(500, receivedMessagesCount); } }