DMS MQTT 客户端使用说明
MQTT选项问题
1:username 使用DMS实例的pub_key
2:password 使用dms实例的sub_key
3:clientid 作为连接标示的,自定义,不能2个连接用同一个clientid,会挤掉线,
4:cleansession 1:标示开始,会对使用相同的clientid的消息进行离线存储。
5:brokerAddress tcp://mqttdms.aodianyun.com:1883
Java库使用实例
连接操作
String serverURI = "tcp://mqttdms.aodianyun.com:1883"
String clientId = "client1";
String pub_key = "demo";
String sub_key = "demo";
MqttAsyncClient client = new MqttAsyncClient(serverURI,clientId,new MemoryPersistence());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(pub_key);
connOpts.setPassword(sub_key.toCharArray());
connOpts.setCleanSession(1);
connOpts.setKeepAliveInterval(30);
IMqttActionListener connCallback
client.connect(connOpts,userContext,connCallback);
MqttCallback callback;
client.setCallback(callback);
关注
int qos = 0;
String topic = "test";
Object userContext = null;
IMqttActionListener subCallback;
client.subscribe(topic,qos,userContext,subCallback);
发布
int qos = 0;
String topic = "test";
String payload = "first message";
Object userContext = null;
IMqttActionListener pubCallback;
client.publish(topic,payload.getBytes("utf-8"),userContext,pubCallback);
断开
Object userContext = null;
IMqttActionListener callback;
client.disconnect(userContext, callback);
client.close();