Esempio n. 1
0
        private static void RunServerAsync()
        {
            try
            {
                var options = new MqttServerOptions
                {
                    ConnectionValidator = p =>
                    {
                        if (p.ClientId == "SpecialClient")
                        {
                            if (p.Username != "USER" || p.Password != "PASS")
                            {
                                return(MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword);
                            }
                        }

                        return(MqttConnectReturnCode.ConnectionAccepted);
                    },
                    DefaultCommunicationTimeout = TimeSpan.FromMinutes(10)
                };

                var mqttServer = new MqttServerFactory().CreateMqttServer(options);
                var msgs       = 0;
                var stopwatch  = Stopwatch.StartNew();
                mqttServer.ApplicationMessageReceived += (sender, args) =>
                {
                    msgs++;
                    if (stopwatch.ElapsedMilliseconds > 1000)
                    {
                        Console.WriteLine($"received {msgs}");
                        msgs = 0;
                        stopwatch.Restart();
                    }
                };
                mqttServer.Start();

                Console.WriteLine("Press any key to exit.");
                Console.ReadLine();

                mqttServer.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }
Esempio n. 2
0
        private static void RunServerAsync(string[] arguments)
        {
            MqttTrace.TraceMessagePublished += (s, e) =>
            {
                Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
                if (e.Exception != null)
                {
                    Console.WriteLine(e.Exception);
                }
            };

            try
            {
                var options = new MqttServerOptions
                {
                    ConnectionValidator = p =>
                    {
                        if (p.ClientId == "SpecialClient")
                        {
                            if (p.Username != "USER" || p.Password != "PASS")
                            {
                                return(MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword);
                            }
                        }

                        return(MqttConnectReturnCode.ConnectionAccepted);
                    }
                };

                var mqttServer = new MqttServerFactory().CreateMqttServer(options);
                mqttServer.Start();

                Console.WriteLine("Press any key to exit.");
                Console.ReadLine();

                mqttServer.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }