Example #1
0
        private static void StartMqttServer()
        {
            if (mqttServer == null)
            {
                try
                {
                    var options = new MqttServerOptions
                    {
                        ConnectionValidator = c =>
                        {
                            //if (c.ClientId.Length < 10)
                            //{
                            //    c.ReturnCode= MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
                            //}

                            if (c.Username != "admin" || c.Password != "123")
                            {
                                c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
                            }
                            c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
                        }
                    };

                    mqttServer = new MqttFactory().CreateMqttServer(new MqttNetLogger()) as MqttServer;
                    mqttServer.ApplicationMessageReceived += MqttServer_ApplicationMessageReceived;
                    mqttServer.ClientConnected            += MqttServer_ClientConnected;
                    mqttServer.ClientDisconnected         += MqttServer_ClientDisconnected;
                    mqttServer.ClientSubscribedTopic      += MqttServer_ClientSubscribedTopic;
                    mqttServer.ClientUnsubscribedTopic    += MqttServer_ClientUnsubscribedTopic;

                    mqttServer.StartAsync(options).Wait();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }


            Console.WriteLine("MQTT服务启动成功!");
        }
Example #2
0
        private static void StartMqttServer()
        {
            if (mqttServer == null)
            {
                try
                {
                    var options = new MqttServerOptions
                    {
                        ConnectionValidator = p =>
                        {
                            if (p.ClientId == "c001")
                            {
                                if (p.Username != "u001" || p.Password != "p001")
                                {
                                    return(MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword);
                                }
                            }

                            return(MqttConnectReturnCode.ConnectionAccepted);
                        }
                    };

                    mqttServer = new MqttServerFactory().CreateMqttServer(options) as MqttServer;
                    mqttServer.ApplicationMessageReceived += MqttServer_ApplicationMessageReceived;
                    mqttServer.ClientConnected            += MqttServer_ClientConnected;
                    mqttServer.ClientDisconnected         += MqttServer_ClientDisconnected;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

            mqttServer.StartAsync();
            Console.WriteLine("MQTT服务启动成功!");
        }
Example #3
0
        public static void StartMqttServer()
        {
            DeviceContext devices = new DeviceContext();

            if (mqttServer == null)
            {
                try
                {
                    var options = new MqttServerOptions
                    {
                        ConnectionValidator = p =>
                        {
                            DeviceModel device = devices.Devices.SingleOrDefault(x => x.DeviceKey == p.Password);
                            if ((device.Name == p.Username) || (device.User == p.Username))
                            {
                                return(MqttConnectReturnCode.ConnectionAccepted);
                            }

                            return(MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword);
                        }
                    };

                    mqttServer = new MqttServerFactory().CreateMqttServer(options) as MqttServer;
                    mqttServer.ApplicationMessageReceived += MqttServer_ApplicationMessageReceived;
                    mqttServer.ClientConnected            += MqttServer_ClientConnected;
                    mqttServer.ClientDisconnected         += MqttServer_ClientDisconnected;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

            mqttServer.StartAsync();
            Console.WriteLine("MQTT服务启动成功!");
        }