コード例 #1
0
ファイル: LogSubscriber.cs プロジェクト: Jaasdsa/CityIoT
        public void Start(Action <Log> actReciveLog, out string errMsg)
        {
            errMsg            = "";
            this.actReciveLog = actReciveLog;
            //日志接收触发器
            this.logReciver = new LogReciver(this.newsTopic, this.actReciveLog);
            this.logReciver.Start();
            if (!this.logReciver.IsRuning)
            {
                Stop();
                return;
            }

            // 启动MQTT客户端
            mqttClient = new MQTTClient(this.param, ReciveMqttClientDataHander);
            mqttClient.Connect(out errMsg);
            if (!string.IsNullOrWhiteSpace(errMsg))
            {
                Stop();
                return;
            }

            if (!this.mqttClient.IsConnected)
            {
                errMsg = "连接MQTT客户端失败";
                Stop();
                return;
            }
            // 订阅日志事件
            this.mqttClient.SubScribe(this.newsTopic, out errMsg);
            if (!string.IsNullOrWhiteSpace(errMsg))
            {
                Stop();
                return;
            }

            //启动维护MQTT维护线程
            //StartKeepAlive();

            IsRuning = true;
        }
コード例 #2
0
ファイル: LogSubscriber.cs プロジェクト: Jaasdsa/CityIoT
        public void Stop()
        {
            //维护MQTT维护线程
            //StopKeepAlive();

            if (this.mqttClient != null)
            {
                try
                {
                    if (this.mqttClient.IsConnected)
                    {
                        this.mqttClient.UnSubScribe(this.newsTopic, out string errMsg);
                    }
                }
                catch { }

                try
                {
                    this.mqttClient.DisConnect();
                }
                catch { }
                this.mqttClient = null;
            }

            if (this.logReciver != null)
            {
                try
                {
                    this.logReciver.Stop();
                    this.logReciver = null;
                }
                catch { }
            }

            IsRuning = false;
        }