Exemple #1
0
        public void when_initializing_server_then_succeeds()
        {
            var configuration = new MqttConfiguration {
                BufferSize = 131072,
                Port       = MqttProtocol.DefaultNonSecurePort
            };
            var binding     = new ServerTcpBinding();
            var initializer = new MqttServerFactory(binding);
            var server      = initializer.CreateServer(configuration);

            Assert.NotNull(server);

            server.Stop();
        }
Exemple #2
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();
        }
Exemple #3
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();
        }
        protected async Task <IMqttServer> GetServerAsync(IMqttAuthenticationProvider authenticationProvider = null)
        {
            try {
                LoadConfiguration();

                var binding     = new ServerTcpBinding();
                var initializer = new MqttServerFactory(binding, authenticationProvider);
                var server      = initializer.CreateServer(Configuration);

                server.Start();

                return(server);
            } catch (MqttException protocolEx) {
                if (protocolEx.InnerException is SocketException)
                {
                    return(await GetServerAsync());
                }
                else
                {
                    throw;
                }
            }
        }
        public void TestMethod1()
        {
            var options = new MqttServerOptions
            {
                ConnectionValidator = c =>
                {
                    if (c.ClientId.Length < 10)
                    {
                        return(MqttConnectReturnCode.ConnectionRefusedIdentifierRejected);
                    }

                    if (c.Username != "xxx" || c.Password != "xxx")
                    {
                        return(MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword);
                    }

                    return(MqttConnectReturnCode.ConnectionAccepted);
                }
            };

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

            mqttServer.StartAsync();
        }