概述
package com.demo.mqtt;
import com.ibm.mqtt.MqttClient;
import com.ibm.mqtt.MqttException;
import com.ibm.mqtt.MqttSimpleCallback;
public class ClientSub {
private final static boolean CLEAN_START = true;
private final static short KEEP_ALIVE = 30;// 低耗网络,但是又需要及时获取数据,心跳30s
private final static String CLIENT_ID = "clientSub";
/**
* @param args
* @throws MqttException
*/
public static void main(String[] args) throws MqttException {
MqttClient mqttClient1 = new MqttClient("tcp://123.57.81.91:1883");
mqttClient1.connect(CLIENT_ID, CLEAN_START, KEEP_ALIVE);
mqttClient1.subscribe(new String[]{"user/2"}, new int[]{2});
mqttClient1.registerSimpleHandler(new MqttSimpleCallback() {
@Override
public void publishArrived(String arg0, byte[] arg1, int arg2, boolean arg3)
throws Exception {
System.out.println(arg0);
System.out.println(new String(arg1));
System.out.println(arg2);
System.out.println(arg3);
}
@Override
public void connectionLost() throws Exception {
System.out.println("4444444444444444444444444444444444444444");
}
});
}
}
最后
以上就是明理镜子为你收集整理的mqtt的ClientSub的全部内容,希望文章能够帮你解决mqtt的ClientSub所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复