/// <summary> /// Constructor /// </summary> /// <param name="region">UIoT-Core Region: cn-sh2</param> /// <param name="productSN">UIoT-Core productSN</param> /// <param name="deviceSN">UIoT-Core deviceSN</param> /// <param name="deviceSecret">UIoT-Core deviceSecret</param> /// <param name="secure">UIoT-Core secure mode:1883/8883</param> /// <param name="caCertFilename">CA certificate filepath for secure connection</param> /// <param name="handler">The Receive callback handler:void MqttMsgPublishEventHandler(object sender, MqttMsgPublishEventArgs e) </param> public UIoTClient(string region, string productSN, string deviceSN, string deviceSecret, bool secure, string caCertFilename, MqttClient.MqttMsgPublishEventHandler handler) { #if !(WINDOWS_APP || WINDOWS_PHONE_APP) X509Certificate caCert; if (caCertFilename == null) { caCert = null; } else { caCert = X509Certificate.CreateFromCertFile(caCertFilename); } mqttclient = new MqttClient("mqtt-" + region + ".iot.ucloud.cn", secure?8883:1883, secure, caCert, null, MqttSslProtocols.TLSv1_2); #else mqttclient = new MqttClient("mqtt-" + region + "iot.ucloud.cn", MqttSettings.MQTT_BROKER_DEFAULT_SSL_PORT, true, MqttSslProtocols.TLSv1_2); #endif this.productSN = productSN; this.deviceSN = deviceSN; this.deviceSecret = deviceSecret; this.region = region; subtopics = new List <string>(); subtopicqoss = new List <byte>(); this.IsConnected = false; this.umqttMsgPublishReceivedHandler = handler; mqttclient.MqttMsgPublishReceived += handler; // 连接断开事件绑定 mqttclient.ConnectionClosed += (sender, e) => { this.IsConnected = false; this.Connect(); }; }
void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { //处理接收到的消息 string msg = System.Text.Encoding.Default.GetString(e.Message); if (this.InvokeRequired) { MqttClient.MqttMsgPublishEventHandler setpos = new MqttClient.MqttMsgPublishEventHandler(Client_MqttMsgPublishReceived); this.Invoke(setpos, new object[] { sender }, e); } else { richTextBox2.AppendText(e.Topic + ":" + msg + "--" + DateTime.Now.ToString() + "\r\n"); } }
/// <summary> /// Constructor /// </summary> /// <param name="region">UIoT-Core Region: cn-sh2</param> /// <param name="productSN">UIoT-Core productSN</param> /// <param name="deviceSN">UIoT-Core deviceSN</param> /// <param name="deviceSecret">UIoT-Core deviceSecret</param> /// <param name="caCertFilename">CA certificate filepath for secure connection</param> /// <param name="handler">The Receive callback handler:void MqttMsgPublishEventHandler(object sender, MqttMsgPublishEventArgs e) </param> public UIoTClient(string region, string productSN, string deviceSN, string deviceSecret, string caCertFilename, MqttClient.MqttMsgPublishEventHandler handler) : this(region, productSN, deviceSN, deviceSecret, true, caCertFilename, handler) { }
/// <summary> /// Constructor /// </summary> /// <param name="region">UIoT-Core Region: cn-sh2</param> /// <param name="productSN">UIoT-Core productSN</param> /// <param name="deviceSN">UIoT-Core deviceSN</param> /// <param name="deviceSecret">UIoT-Core deviceSecret</param> /// <param name="handler">The Receive callback handler:void MqttMsgPublishEventHandler(object sender, MqttMsgPublishEventArgs e) </param> public UIoTClient(string region, string productSN, string deviceSN, string deviceSecret, MqttClient.MqttMsgPublishEventHandler handler) : this(region, productSN, deviceSN, deviceSecret, true, null, handler) { }