MQTT for UWP, mqttforuwp

Source: Internet
Author: User

MQTT for UWP, mqttforuwp

Old Rules: Let's briefly introduce MQTT:

MQTT (Message Queuing Telemetry Transport, Message Queue Telemetry transmission) is an instant messaging protocol developed by IBM and may become an important part of Iot. The protocol supports almost all platforms and connects almost all networked items to external devices. It will be used as a communication protocol for sensors and actuators (such as connecting houses to the Internet through Twitter) over the next few years, MQTT applications will become more and more popular and worth noting.

Through the MQTT protocol, dozens of MQTT server programs have been extended. You can send related messages to MQTT through PHP, JAVA, Python, C, C #, and other system languages. In addition, many enterprises in China use MQTT as the protocol for pushing messages to Android mobile clients and servers. Among them, Sohu and Cmstop mobile clients use MQTT as message push messages. According to Li Wenkai, a senior R & D Engineer responsible for message push in Cmstop, due to the open source code and low power consumption characteristics of MQTT, with the development of mobile Internet, more contributions will be made to the mobile message push field. In the IOT field, communication between sensors and servers, information collection, and MQTT can all be considered. In the future, MQTT will go into various aspects of our life (from Baidu encyclopedia ).

Obviously, UWP also has MQTT. to use it, you need to install the M2Mqtt Nuget package. Reference

Using uPLibrary. Networking. M2Mqtt;
Using uPLibrary. Networking. M2Mqtt. Messages;

The use of Mqtt is very simple. First, a new MqttClient is created, Connect and Subscribe to messages, and then you can receive messages from the subscribed room. You can Publish messages through Publish.

The specific implementation is as follows:

Public void ConnMqtt () {string mqtthost = "your ip address"; int mqttport = your port; string mqttServer = "your server"; mc. connect (mqttServer); // mc = new MqttClient (mqtthost, mqttport, false, MqttSslProtocols. none); mc. mqttmsgpublishinclued + = mc_mqttmsgpublishinclued; try {string clientId = Guid. newGuid (). toString (); mc. connect (clientId); // mc. connect (clientId, "username", "password");} catch (Exception ex) {Debug. writeLine (ex. message);} SubscribeMqtt (new [] {"test"}); // subscription} public void SubscribeMqtt (string [] topics) // subscribe to {mc. subscribe (topics, new [] {MqttMsgBase. QOS_LEVEL_AT_MOST_ONCE});} public void PublishMqtt (string topic, string message) // publish the message. You can also receive the message {var v = mc. publish (topic, Encoding. UTF8.GetBytes (message), MqttMsgBase. QOS_LEVEL_AT_MOST_ONCE, true);} async private void mc_mqttmsgpublishencoded ed (object sender, MqttMsgPublishEventArgs e) {var msg = Encoding. UTF8.GetString (e. message); await this. dispatcher. runAsync (Windows. UI. core. coreDispatcherPriority. normal, () => {ReciveTextBox. text + = msg + "\ r \ n ";});}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.