Exemplo n.º 1
0
        // MQTT 消息发布方法
        public void PublishAsync(MQTTTopicData data, out string errMsg)
        {
            errMsg = "";
            if (data == null)
            {
                errMsg = "发布的数据包错误";
                return;
            }
            if (data.topic == null)
            {
                errMsg = "发布的数据主题不能为空";
                return;
            }
            if (data.palyLoad == null)
            {
                errMsg = "发布的数据包有效载荷不能为空";
                return;
            }
            if (!mqttClient.IsConnected)
            {
                errMsg = "MQTT客户端尚未连接!";
                return;
            }
            MqttApplicationMessage message = new MqttApplicationMessage();

            message.Topic   = data.topic;
            message.Payload = data.palyLoad;
            message.QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce;
            message.Retain = false;
            Task task = mqttClient.PublishAsync(message);

            task.Wait(5000);// 最多只等待5秒
        }
Exemplo n.º 2
0
 private void ReciveMqttClientDataHander(MQTTTopicData data)
 {
     if (this.logReciver != null && this.logReciver.IsRuning)
     {
         this.logReciver.Append(data);
     }
 }
Exemplo n.º 3
0
        private void MqttClient_ApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
        {
            MQTTTopicData data = new MQTTTopicData();

            data.topic    = e.ApplicationMessage.Topic;
            data.palyLoad = e.ApplicationMessage.Payload;
            actionReciveData(data);// 触发接受事件
        }
Exemplo n.º 4
0
        public void Append(MQTTTopicData data)
        {
            if (!IsRuning)
            {
                return;
            }
            if (queue.IsAddingCompleted)
            {
                return;
            }
            if (queue.Count > 4096)
            {
                return;
            }

            queue.Add(data);
        }
Exemplo n.º 5
0
        private void Excute(MQTTTopicData data)
        {
            if (data == null)
            {
                return;
            }

            // 只接收机日志消息
            if (data.topic != this.logNewsTopic)
            {
                return;
            }
            if (data.palyLoad == null)
            {
                return;
            }

            Log log = ByteUtil.ToDeserializeObject <Log>(data.palyLoad);

            actionReciveLog(log);
        }