For a long time did not write things, six months to engage in 1.5 app, the 2 iPad project, people are crazy….
Today in refactoring code, summarize: MQTT usage and pit
MQTT is an instant messaging protocol developed by IBM, it is likely to become an important part of the Internet of things. The protocol supports almost all platforms, can connect all networking items and external, to be used as sensors and actuators (such as through the Twitter network communication protocol for housing) conducting Baidu entry interpretation
MQTT is a very well designed transport layer protocol, which has a wide range of applications in the field of mobile messaging, Internet of things, car networking, intelligent hardware and energy exploration. 1 byte header, 2 bytes heartbeat, message QoS support design, very suitable for low bandwidth, unreliable networks, embedded devices.
Different applications have different requirements of the system, the user can use the emqttd messaging server before, can be tested according to their application scenarios, rather than a simple connection stress test:
Android message push: push message broadcast test.
Mobile instant messaging applications: messaging confirmation test.
Intelligent hardware applications: round trip delay test.
Internet of things data collection: concurrent connection and throughput test
Not much to say, there are a lot of information, commonly used in two MQTT
Two have been used, but the current use of MQTTClient, after the Ios10 out of the updated version, regular maintenance;
MQTTKit, this looks like a long time did not update, not to say
First of all, clear what you do with this thing, take me to do app:
On the map page request, historical data, and then through the mqtt access terminal to subscribe to topics, return data, and update data. The effect is similar to a taxi software, to achieve real-time monitoring data and other functions.
Step 1: install MQTT
MQTT-Client-Framework GitHub address
Cocopod direct download, good pod’MQTTClient’
After installation, so that each class to see what is to know, directly on the code in detail
Import #import< MQTTClient/MQTTClient>
1 set the address and port number
2 set the mqtt account and password, the same to find a good base to friends
The last 3 subscription theme, this place to see a lot of people blog, if you lots of themes such as 5, 10, the use of thread processing, this is also possible, but not optimal, behind the details, the mqtt is also can subscribe to multiple topics, many data are not stated.
This place with the enumeration, mainly in order to determine the subscription theme, to deal with callback data
Note: / / subscribe to a topic can not put the thread, otherwise block will not react
These are / have their own package
The theme of format / @ $IOT/haha/datapoint/motor_control”
If this is not / / package
This will achieve the theme of subscription, subscription success will have log, a pile of information, such a
The fourth step: to achieve Session proxy method, data processing
In the proxy method, you can get the corresponding data, here to explain
1 if you subscribe to only one theme, then you do not judge is also possible, but if there are multiple topics, you need to determine, which to return to the subject, and then can handle data.
2 do you subscribe to a topic, if this is the @ “$IOT/haha/datapoint/motor_control”, so when processing the data, you judge the string “motor_control” can be found in the corresponding data, determine the final parameters on the line, look at the screenshot to understand.
3 then get your data, what do you want to do
(* o *)… Here to the basic on the end, it is impossible, I found a lot of information to write here is over, there are a lot of focus did not explain, otherwise it would be meaningless to sort things out
We may not use the same, some people will encounter, some people will not be here to explain, may encounter problems:
1 first to say common attributes, if you have been mqtt open, you do not deal with, then mqtt will always have data to return, my side is a second, and then a pile of data…
In the beginning, the initialization of the MQTTSession object, see screenshot
[self.mySession disconnect] disconnect is the corresponding [self.mySession connect] reconnect
If you do not need mqtt, remember close, otherwise mqtt will return data.
Of course, this screenshot, but all examples, if you do not have mqtt directly, close get it, do not need to write so much
2 under normal circumstances, your mqtt successfully subscribed to the theme, then it will have been monitoring the server whether the data returned, but some time, you will encounter this situation, mqtt stopped, of course, remove your own close case.
This situation is very pit father, you use the purpose of mqtt is to monitor the real time….
There are several explanations for the information I have:
The premise is that your home server has been sending data rather than stopping it
2.1 your clientID, there is no set, a simple description, you have an account is Pharaoh, so you log in, and your colleagues are logged in, this time, mqtt will automatically drop.
Self.mySession.clientId has this property, I consulted the background, said mobile terminal to set, set the success can be avoided, but if you are using MQTTClient, this, no, because someone else has already been dealt with, you can click on the go in, so if you are using other third party, clientId needs to take a random number that is not the same.
2.2 encountered above this problem do not MQTTClient reconnection mechanism, maybe I did not find a buddy to see please tell me.
This time, you need to monitor the state of the message, you can deal with, to see if the value of self.mySession.status
In the above also wrote, if you use disconnect/connect, then the state of the message will eventually output fifth MQTTSessionStatusClosed.
2.3 introduction of the two monitoring methods:
2.3.1 KVO when creating the MQTTSession object, you can add
As long as the message is closed, then mqtt will immediately reconnect; but there is a problem, if you mqtt is only part of the page, then leave this page, mqtt will continue to monitor, constantly create objects, as explained above, the final message status is MQTTSessionStatusClosed.
2.3.2 I am currently using a timer, using a timer to deal with message status
The advantage is that the time to leave the page, mqtt off, the timer is also closed, will not lead to the kind of situation KVO, the downside is not found
This allows mqtt to be in a state of control
3 on the MQTT one-time subscription multiple themes, each mqtt framework can be.
If you want to subscribe to 10 themes at once, then you can’t write it ten times, no need to do so, and now introduce a simpler approach,
You just need to change a place, URL
The theme I wrote above is this
The theme of format / @ “$IOT/haha/datapoint/motor_control”
Now you just need to take URL theme to this @ $IOT/haha/#”
MQTT (Topic) to support the ‘+’, ‘#’ wildcard ‘+’ with a level of ‘#’ with multiple levels (must be at the end of
Explain, # said that all of the topics of haha below, if you have 10 themes, you can complete the one-time subscription, then you return data, determine the corresponding theme processing data.
The code will be able to comment on the other
Then again, a page you only use a theme of the content, do you also write so, so everything to see the actual situation.
4 finally say one, MQTT timeout setting problem
MQTT in the network situation is not very good / can not receive server data, easy to cause the thread stuck, you know, what can not point, it is recommended to set the time here a little smaller
Finally, add a background site, setting up the mqtt server, the mobile terminal using the principle can be used, MQTT introduced a very comprehensive, in-depth understanding of the swift and OC can see, this version, I have not used, the same principle.
MQTT-EMQ server build
6.DEMO download address no, then again