async Task <ManagedMqttClient> CreateManagedClientAsync(
            TestEnvironment testEnvironment,
            MqttClient underlyingClient      = null,
            TimeSpan?connectionCheckInterval = null,
            string host = "localhost")
        {
            await testEnvironment.StartServer();

            var clientOptions = new MqttClientOptionsBuilder().WithTcpServer(host, testEnvironment.ServerPort);

            var managedOptions = new ManagedMqttClientOptionsBuilder().WithClientOptions(clientOptions).Build();

            // Use a short connection check interval so that subscription operations are performed quickly
            // in order to verify against a previous implementation that performed subscriptions only
            // at connection check intervals
            managedOptions.ConnectionCheckInterval = connectionCheckInterval ?? TimeSpan.FromSeconds(0.1);

            var managedClient = new ManagedMqttClient(underlyingClient ?? testEnvironment.CreateClient(), new MqttNetEventLogger());

            var connected = GetConnectedTask(managedClient);

            await managedClient.StartAsync(managedOptions);

            await connected;

            return(managedClient);
        }
示例#2
0
        public void Setup()
        {
            TopicGenerator.Generate(NumPublishers, NumTopicsPerPublisher, out var topicsByPublisher, out var singleWildcardTopicsByPublisher, out var multiWildcardTopicsByPublisher);
            _topics = topicsByPublisher.Values.First();

            var serverOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();

            var factory = new MqttFactory();

            _mqttServer = factory.CreateMqttServer(serverOptions);
            _mqttClient = factory.CreateMqttClient();

            _mqttServer.StartAsync().GetAwaiter().GetResult();

            var clientOptions = new MqttClientOptionsBuilder()
                                .WithTcpServer("localhost").Build();

            _mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult();

            foreach (var topic in _topics)
            {
                var subscribeOptions = new MqttClientSubscribeOptionsBuilder()
                                       .WithTopicFilter(topic, Protocol.MqttQualityOfServiceLevel.AtMostOnce)
                                       .Build();
                _mqttClient.SubscribeAsync(subscribeOptions).GetAwaiter().GetResult();
            }
        }
示例#3
0
        /// <summary>
        /// Constructor of client and serializer
        /// </summary>
        /// <param name="InternalClientInfo"></param>
        public MqttClient(ConnectConfig config)
        {
            _config = config;
            //建立MqttClient物件
            _client = new MqttFactory().CreateMqttClient() as MQTTnet.Client.MqttClient;
            _client.Disconnected += MqttClient_Disconnected;
            _mqttStatus           = MqttBrokerConnectionStatus.DisConnected;

            _mqttClientRunningThread = new Thread(new ThreadStart(mqttClientRunningProcess));
        }
示例#4
0
        public ManagedMqttClient(MqttClient mqttClient, IMqttNetLogger logger)
        {
            InternalClient = mqttClient ?? throw new ArgumentNullException(nameof(mqttClient));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            _logger = logger.WithSource(nameof(ManagedMqttClient));
        }
示例#5
0
        public void Setup()
        {
            TopicGenerator.Generate(NumPublishers, NumTopicsPerPublisher, out var topicsByPublisher, out var singleWildcardTopicsByPublisher, out var multiWildcardTopicsByPublisher);
            _topics = topicsByPublisher.Values.First();

            var serverOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();

            var factory = new MqttFactory();

            _mqttServer = factory.CreateMqttServer(serverOptions);
            _mqttClient = factory.CreateMqttClient();

            _mqttServer.StartAsync().GetAwaiter().GetResult();

            var clientOptions = new MqttClientOptionsBuilder()
                                .WithTcpServer("localhost").Build();

            _mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult();
        }
示例#6
0
        public void Setup()
        {
            var serverOptions = new MqttServerOptionsBuilder().Build();

            var factory = new MqttFactory();

            _mqttServer = factory.CreateMqttServer(serverOptions);
            _mqttClient = factory.CreateMqttClient();

            _mqttServer.StartAsync().GetAwaiter().GetResult();

            var clientOptions = new MqttClientOptionsBuilder()
                                .WithTcpServer("localhost").Build();

            _mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult();

            _message = new MqttApplicationMessageBuilder()
                       .WithTopic("A")
                       .Build();
        }
 static Task PublishSingleMessage(MqttClient client, MqttApplicationMessage applicationMessage, ref int count)
 {
     Interlocked.Increment(ref count);
     return(Task.Run(() => client.PublishAsync(applicationMessage)));
 }
示例#8
0
 public TestApplicationMessageReceivedHandler CreateApplicationMessageHandler(MqttClient mqttClient)
 {
     return(new TestApplicationMessageReceivedHandler(mqttClient));
 }