C# uses MQTT protocol to realize communication

 1. What is the MQTT protocol

 The full name of the MQTT protocol is  Message Queuing Telemetry Transport , which is translated as message queue transmission detection. It is a message protocol based on publish-subscribe mode under the ISO standard. It is based on the TCP/IP protocol cluster. It is to improve network equipment. The performance of the hardware and the performance of the network are designed. MQTT is generally used in IoT, or the Internet of Things, and is widely used in industrial-level application scenarios, such as automobiles, manufacturing, oil, and natural gas.

2. Advantages and disadvantages of MQTT protocol

advantage: 

  1. Easier: MQTT is a message queuing protocol that uses the publish/subscribe message mode to provide one-to-many message publishing and decouple application programs. Compared with other protocols, development is simpler;
  2. Network stability: work on the TCP/IP protocol; the TCP/IP protocol provides a stable network connection;
  3. Lightweight: small-scale transmission, low overhead (fixed-length header is 2 bytes), minimized protocol exchange to reduce network traffic; suitable for applications with low bandwidth and small data volume;
  4. Easy to implement: The server program of the MQTT protocol is very mature, and system languages ​​such as PHP, JAVA, Python, C, and C# can all send relevant messages to MQTT;
  5. Openness: The source code is open and can be used, which further promotes the development of MQTT. Almost all open IoT platforms such as Baidu Cloud, Alibaba Cloud, and China Mobile Onenet support MQTT;

shortcoming:

  1. File and AV are not supported
  2. Does not support integration with third-party HTTP
  3. Does not support load sharing
  4. Does not support user management interface
  5. Does not support point-to-point communication, and uses the standard MQTT protocol
  6. Does not support group communication or group management, but implements the management of group members

3. Understanding of MQTT client and MQTT server

1. MQTT client: Subscribe to the server for the corresponding topic message content, and obtain the subscribed related topic message or publish the related topic message to the server.

2. MQTT server: mainly used to maintain connections with multiple clients, and handle client (client) publishing and subscription logic. Generally, it is rare to publish messages directly from the server (server) to the client (if you want to publish a message, you can use the  mqttServer.Publish(appMsg); direct release message to the client). In most cases, the server forwards the topic message that the client has subscribed to the corresponding client. In the system play an intermediary role.

 4. The connection between MQTT protocol and TCP/IP protocol

  1. The MQTT protocol is a protocol belonging to the application layer
  2. The TCP/IP protocol is a protocol belonging to the transport layer 

5.MQTT Serve and MQTT Client publish and subscribe message test

1. First download the MQTT Serve test tool HslCommunication

1.1 Start an MQTT server

 1.2 Open another test client and connect to it

1.3 Open our writing client, the port number is also 1883 

 1.4 On the server side, you can see that 2 clients have connected

 2. Perform message publishing and subscription testing

 2.1 Client 1, subscribe to a topic whose topic message is banana. Client 2, subscribes to a topic message as Apple's topic. How to understand this test? That is, when the publisher of the message publishes the message, if the subject is banana, the server will only send the message to client 1, and client 2 will not receive the message whose subject is banana. Understand!

2.2 Client 2 starts publishing topic messages

 2.3 Client 1 publishes topic message

 2.3 The server publishes a message to the client

Summarize:

1. The server can also publish messages to the client, but the role of the server is generally for message forwarding. And forward the corresponding topic message to the corresponding client.

2. The client can be a subscriber of the message or a publisher of the message

The source code has been uploaded to CSDN, an open source project innovation service platform gitcode for developers

 references:

https://zhuanlan.zhihu.com/p/268113708

https://zhuanlan.zhihu.com/p/342874157

https://blog.csdn.net/panwen1111/article/details/79245161

https://blog.csdn.net/yuming/article/details/125834921

Guess you like

Origin blog.csdn.net/weixin_39237340/article/details/128906164