コード例 #1
0
        public MqttServer(string CaCertPath, string ServerIP, string ServerPort, string ServerEui)
        {
            X509Certificate CaCert = X509Certificate.CreateFromCertFile(CaCertPath); //注意证书文件不存在会导致相关Man初始化失败

            MqttClient = new MQTTMan(ServerEui, ServerEui, ServerIP, int.Parse(ServerPort), CaCert, MqttSslProtocols.TLSv1_2);
            MqttClient.Start();
            MqttClient.RcvDataEvent += MqttClient_RcvDataEvent;
        }
コード例 #2
0
        public void Start()
        {
            IsRunning = true;
            Console.WriteLine("*************服务器开始启动");
            try
            {
                X509Certificate CaCert = X509Certificate.CreateFromCertFile(CaCertPath); //注意证书文件不存在会导致相关Man初始化失败
                MqttClient = new MQTTMan(ClientEui.ToHexString(), ServerEui, ServerIP, int.Parse(ServerPort), CaCert, MqttSslProtocols.TLSv1_2);
                MqttClient.Start();
                MqttClient.RcvDataEvent += MqttClient_RcvDataEvent;

                Task.Run(() =>
                {
                    while (IsRunning)
                    {
                        try
                        {
                            MqttClient?.Request(Package_0x00());
                            Thread.Sleep(1000);
                        }
                        catch { }
                    }
                });
                Task.Run(() =>
                {
                    while (IsRunning)
                    {
                        try
                        {
                            if (Status == 1 && IsSession)
                            {
                                MqttClient?.Request(OtherCommidStr, Package_0x02(GetImage()));
                            }
                            Thread.Sleep(100);
                        }
                        catch { }
                    }
                });
            }
            catch (Exception ex) { Console.WriteLine(ex + " ca:" + CaCertPath); }
            Console.WriteLine("*************服务器启动完成");
        }