Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to re-package MQTT open source component moquette

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to re-package MQTT open source components moquette", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn how to re-package MQTT open source components moquette!

What is moquette?

Moquette is an open source message agent, the whole system is based on java development, based on netty for the complete implementation of the MQTT protocol.

The purpose of encapsulation

It is really not easy to use moquette directly on the project, and it is more convenient to do a certain amount of encapsulation through the requirements.

Let's start to record the encapsulation of moquette.

Encapsulated content

1. Encapsulation mainly involves following SpringBoot startup and starting mqtt server at the same time.

two。 The encapsulation server sends messages to the client, encapsulates whether the client is online, and the encapsulation server sends messages to the device and gets the return value.

Directory: under java directory:

Config: configuration class file

Handler: client connection and message processing class files

Server: message sending class that encapsulates moquette

Utils: the tool class to be used

Under the resources directory:

Mqtt: Port, account number, secret and other configuration information of mqtt service

Configure the class file:

MqttBrokerConfiguration.java: Server that creates moquette automatically injects objects in Spring

The MqttBrokerRunConfiguration.java:mqtt service starts the configuration class with the SpringBoot project

MqttCustomConfig.java: the Topic used to manage mqtt

Client connection handling class:

The following two classes deal with the client's event functions such as connecting, disconnecting and receiving client messages. They mainly distinguish different usage scenarios and mainly implement the AbstractInterceptHandler class provided by moquette.

Send a message to the client class:

Instructions: 1. Receive client data API: implement the AbstractInterceptHandler class to override the onPublish function to receive messages from the client through the onPublish function. two。 Determine whether the client is online APIMqttServer.isOnline (clientId) 3. Send a message to the client this method is suitable for the device to provide message transmission such as interface and parameter type, and will get the corresponding return value, but the client needs to push a specific topic message to the server after the client has processed the data. The two sides agree on a good message topicMqttServer.luatInvoke (clientId,apiTag,params) clientId: client IDapiTag: client interface name params: parameter this method is applicable to messages such as no interface provided by the client. And the corresponding return value will be obtained, but the client needs to push a specific topic message to the server after processing the data. The two parties agree on a good message topicMqttServer.pubAndSub (clientId,rrpcCmd,topicPub,topicSub) clientId: client IDrrpcCmd: message sent topicPub: topictopicSub subscribed by the client: topic sent by the message

The MQTT client connects to the server through the TCP protocol and sends messages to the server. The server's AbstractInterceptHandler.onPublish (msg) interface can receive any message sent from each client. At the same time, the client needs to listen to the callback function to ensure that it receives the message from the server. After sending a message to the client, the MQTT server has 8 seconds to wait for the client to publish the specified topic message. When the server receives it, it treats it as a return value.

Encapsulated code path: https://gitee.com/winfongs/mqtt

At this point, I believe you have a deeper understanding of "how to re-package MQTT open source components moquette". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report