C # accepts messages pushed by MQTT server

Source: Internet
Author: User

Objective:

MQTT is an Instant Messaging protocol developed by IBM. MQTT is a connectivity protocol for both the machine and the internet of Things, with lightweight publishing and subscription messaging.

You can directly on GitHub download MQQT service source code, source address: https://github.com/mqtt/mqtt.github.io/wiki/libraries

Main content:

Official Document Translation:


The M2MQTT library provides a main class mqttclient that represents the MQTT client that connects to the agent. You can connect to an agent that provides its IP address or host name, and optionally some parameters related to the MQTT protocol.

After you connect to the agent, you can use the Publish () method to publish messages to the topic and the subscribe () method to subscribe to the topic and receive messages posted on it.

The Mqttclient class is event-based so that you receive an event when a message is published to a topic that you subscribe to. After the message is published, you can receive the event that you subscribed to or unsubscribed from the topic.

Examples of client-based topics:

...

Create Client Instance
Mqttclient client = new Mqttclient (Ipaddress.parse (mqtt_broker_address));

Register to message received
Client. Mqttmsgpublishreceived + = client_mqttmsgpublishreceived;

String clientId = Guid.NewGuid (). ToString ();
Client. Connect (CLIENTID);

Subscribe to the topic "/home/temperature" with QoS 2
Client. Subscribe (new string[] {"/home/temperature"}, new byte[] {mqttmsgbase.qos_level_exactly_once});

...

static void Client_mqttmsgpublishreceived (object sender, Mqttmsgpublisheventargs e)
{
Handle Message Received
}

In general, C # clients are applying a singleton pattern, and the following is the class I encapsulate:

1    Public classMqttclientservice2     {3 4         Private Static volatileMqttclientservice _instance =NULL;5 6         Private Static ReadOnly ObjectLockhelper =New Object();7 8         /// <summary>9         ///Create a singleton modeTen         /// </summary> One         /// <param name= "ipAddress" ></param> A         /// <returns></returns> -          Public StaticMqttclientservice CreateInstance (stringipAddress) -         { the             if(_instance = =NULL) -             { -                 Lock(Lockhelper) -                 { +                     if(_instance = =NULL) -_instance =NewMqttclientservice (ipAddress); +                 } A             } at             return_instance; -         } -  -         /// <summary> -         ///instantiating a subscription client -         /// </summary> in          PublicMqttclient Subscribeclient {Get;Set; } -  to  +          PublicAction<object, mqttmsgpublisheventargs> clientpublishreceivedaction {Get;Set; } -  the          PublicMqttclientservice (stringipAddress) *         { $             //Create client InstancePanax NotoginsengSubscribeclient =Newmqttclient (Ipaddress.parse (IPAddress)); -  the             //register to message received +Subscribeclient.mqttmsgpublishreceived + =client_mqttmsgpublishreceived; A  the             stringClientId =Guid.NewGuid (). ToString (); +  - Subscribeclient.connect (clientId); $  $             //Subscribe to the topic "/home/temperature" with QoS 2 -Subscribeclient.subscribe (New string[] {"avatar/uploaded"},New byte[] {mqttmsgbase.qos_level_exactly_once}); -         } the  -         voidClient_mqttmsgpublishreceived (Objectsender, Mqttmsgpublisheventargs e)Wuyi         { the             //Handle Message Received - Clientpublishreceivedaction.invoke (sender, E); Wu         } -  About          Public voidClient_mqttmsgpublish (stringpublishstring) $         { -Subscribeclient.publish ("avatar/signed", Encoding.UTF8.GetBytes (publishstring), Mqttmsgbase.qos_level_exactly_once,false); -         } -}

The user only needs to write the path of the subscription to subscribe.

C # accepts messages pushed by MQTT server

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.