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 ";});}