2019独角兽企业重金招聘Python工程师标准>>>
SimpleMqttClient.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.MqttTopic; public class SimpleMqttClient implements MqttCallback { MqttClient myClient; MqttConnectOptions connOpt; static final String BROKER_URL = "tcp://q.m2m.io:1883"; static final String M2MIO_DOMAIN = "<Insert m2m.io domain here>"; static final String M2MIO_STUFF = "things"; static final String M2MIO_THING = "<Unique device ID>"; static final String M2MIO_USERNAME = "<m2m.io username>"; static final String M2MIO_PASSWORD_MD5 = "<m2m.io password (MD5 sum of password)>"; // the following two flags control whether this example is a publisher, a subscriber or both static final Boolean subscriber = true; static final Boolean publisher = true; /** * * connectionLost * This callback is invoked upon losing the MQTT connection. * */ @Override public void connectionLost(Throwable t) { System.out.println("Connection lost!"); // code to reconnect to the broker would go here if desired } /** * * deliveryComplete * This callback is invoked when a message published by this client * is successfully received by the broker. * */ @Override public void deliveryComplete(MqttDeliveryToken token) { //System.out.println("Pub complete" + new String(token.getMessage().getPayload())); } /** * * messageArrived * This callback is invoked when a message is received on a subscribed topic. * */ @Override public void messageArrived(MqttTopic topic, MqttMessage message) throws Exception { System.out.println("-------------------------------------------------"); System.out.println("| Topic:" + topic.getName()); System.out.println("| Message: " + new String(message.getPayload())); System.out.println("-------------------------------------------------"); } /** * * MAIN * */ public static void main(String[] args) { SimpleMqttClient smc = new SimpleMqttClient(); smc.runClient(); } /** * * runClient * The main functionality of this simple example. * Create a MQTT client, connect to broker, pub/sub, disconnect. * */ public void runClient() { // setup MQTT Client String clientID = M2MIO_THING; connOpt = new MqttConnectOptions(); connOpt.setCleanSession(true); connOpt.setKeepAliveInterval(30); connOpt.setUserName(M2MIO_USERNAME); connOpt.setPassword(M2MIO_PASSWORD_MD5.toCharArray()); // Connect to Broker try { myClient = new MqttClient(BROKER_URL, clientID); myClient.setCallback(this); myClient.connect(connOpt); } catch (MqttException e) { e.printStackTrace(); System.exit(-1); } System.out.println("Connected to " + BROKER_URL); // setup topic // topics on m2m.io are in the form <domain>/<stuff>/<thing> String myTopic = M2MIO_DOMAIN + "/" + M2MIO_STUFF + "/" + M2MIO_THING; MqttTopic topic = myClient.getTopic(myTopic); // subscribe to topic if subscriber if (subscriber) { try { int subQoS = 0; myClient.subscribe(myTopic, subQoS); } catch (Exception e) { e.printStackTrace(); } } // publish messages if publisher if (publisher) { for (int i=1; i<=10; i++) { String pubMsg = "{"pubmsg":" + i + "}"; int pubQoS = 0; MqttMessage message = new MqttMessage(pubMsg.getBytes()); message.setQos(pubQoS); message.setRetained(false); // Publish the message System.out.println("Publishing to topic "" + topic + "" qos " + pubQoS); MqttDeliveryToken token = null; try { // publish message to broker token = topic.publish(message); // Wait until the message has been delivered to the broker token.waitForCompletion(); Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } } // disconnect try { // wait to ensure subscribed messages are delivered if (subscriber) { Thread.sleep(5000); } myClient.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
转载于:https://my.oschina.net/youway/blog/491759
最后
以上就是直率豌豆最近收集整理的关于MQTT客户端编码示例-SimpleMqttClient的全部内容,更多相关MQTT客户端编码示例-SimpleMqttClient内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复