예제 #1
0
        /// <summary>
        /// 6
        /// </summary>
        /// <returns></returns>
        static async Task ServerAndClientTestRunAsync()
        {
            MqttNetConsoleLogger.ForwardToConsole();

            var factory = new MqttFactory();

            var server = factory.CreateMqttServer();
            var client = factory.CreateMqttClient();


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

            server.ApplicationMessageReceived += Server_ApplicationMessageReceived;
            await server.StartAsync(serverOptions);

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

            client.ApplicationMessageReceived += Client_ApplicationMessageReceived;


            await client.ConnectAsync(clientOptions);

            Task.Run(() =>
            {
                while (client.IsConnected)
                {
                    client.PublishAsync("test/topic", "hello").GetAwaiter().GetResult();
                    Thread.Sleep(500);
                }
            });

            client.SubscribeAsync("test/topic");
        }
예제 #2
0
        public static async Task RunAsync()
        {
            MqttNetConsoleLogger.ForwardToConsole();

            var factory = new MqttFactory();
            var server  = factory.CreateMqttServer();
            var client  = factory.CreateMqttClient();

            var serverOptions = new MqttServerOptionsBuilder().Build();
            await server.StartAsync(serverOptions);

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

            await Task.Delay(Timeout.Infinite);
        }
예제 #3
0
        /// <summary>
        /// 7
        /// </summary>
        /// <returns></returns>
        static async Task ClientFlowTest()
        {
            MqttNetConsoleLogger.ForwardToConsole();
            try
            {
                var factory = new MqttFactory();
                var client  = factory.CreateMqttClient();

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

                Console.WriteLine("BEFORE CONNECT");
                await client.ConnectAsync(options);

                Console.WriteLine("AFTER CONNECT");

                Console.WriteLine("BEFORE SUBSCRIBE");
                await client.SubscribeAsync("test/topic");

                Console.WriteLine("AFTER SUBSCRIBE");

                Console.WriteLine("BEFORE PUBLISH");
                await client.PublishAsync("test/topic", "payload");

                Console.WriteLine("AFTER PUBLISH");

                await Task.Delay(1000);

                Console.WriteLine("BEFORE DISCONNECT");
                await client.DisconnectAsync();

                Console.WriteLine("AFTER DISCONNECT");

                Console.WriteLine("FINISHED");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
예제 #4
0
파일: ClientTest.cs 프로젝트: zjmsky/SAEA
        public static async Task RunAsync()
        {
            try
            {
                MqttNetConsoleLogger.ForwardToConsole();

                var factory       = new MqttFactory();
                var client        = factory.CreateMqttClient();
                var clientOptions = new MqttClientOptions
                {
                    ChannelOptions = new MqttClientTcpOptions
                    {
                        Server = "127.0.0.1"
                    }
                };

                client.ApplicationMessageReceived += (s, e) =>
                {
                    Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
                    Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
                    Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
                    Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
                    Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
                    Console.WriteLine();
                };

                client.Connected += async(s, e) =>
                {
                    Console.WriteLine("### CONNECTED WITH SERVER ###");

                    await client.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());

                    Console.WriteLine("### SUBSCRIBED ###");
                };

                client.Disconnected += async(s, e) =>
                {
                    Console.WriteLine("### DISCONNECTED FROM SERVER ###");
                    await Task.Delay(TimeSpan.FromSeconds(5));

                    try
                    {
                        await client.ConnectAsync(clientOptions);
                    }
                    catch
                    {
                        Console.WriteLine("### RECONNECTING FAILED ###");
                    }
                };

                try
                {
                    await client.ConnectAsync(clientOptions);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
                }

                Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");

                while (true)
                {
                    Console.ReadLine();

                    await client.SubscribeAsync(new TopicFilter("test", MqttQualityOfServiceLevel.AtMostOnce));

                    var applicationMessage = new MqttMessageBuilder()
                                             .WithTopic("A/B/C")
                                             .WithPayload("Hello World")
                                             .WithAtLeastOnceQoS()
                                             .Build();

                    await client.PublishAsync(applicationMessage);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }