实例介绍
【实例简介】
【实例截图】
【核心代码】
package cn.tyys.mqttbroker.client;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import 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;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
/**
* Created by lhq on 2018/5/21.
*/
public class ApolloClient {
private static String host;
private static String userName = "admin";
private static String passWord = "password";
private static MqttClient client;
private Handler handler;
private static String send_topic ;
private static String receive_topic ;
public ApolloClient(Handler handler,String host,String send_topic,String receive_topic) {
this.handler=handler;
this.host=host;
this.send_topic=send_topic;
this.receive_topic=receive_topic;
}
public void createContent() throws MqttException {
//host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,
//MemoryPersistence设置clientid的保存形式,默认为以内存保存
client = new MqttClient(host, "client", new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
//设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,
//这里设置为true表示每次连接到服务器都以新的身份连接
options.setCleanSession(true);
//设置连接的用户名
options.setUserName(userName);
//设置连接的密码
options.setPassword(passWord.toCharArray());
// 设置超时时间 单位为秒
options.setConnectionTimeout(10);
// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
options.setKeepAliveInterval(20);
//回调
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
// //连接丢失后,一般在这里面进行重连
Log.d("lhq","connectionLost----------");
Message obtainMessage = handler.obtainMessage();
obtainMessage.what=0;
obtainMessage.obj=cause.getMessage();
handler.sendMessage(obtainMessage);
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
//subscribe后得到的消息会执行到这里面
Log.d("lhq","messageArrived----------");
Log.d("lhq",topic "---" message.toString());
Message obtainMessage = handler.obtainMessage();
obtainMessage.what=1;
obtainMessage.obj=message.toString();
handler.sendMessage(obtainMessage);
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//publish后会执行到这里
Log.d("lhq","deliveryComplete---------" token.isComplete());
}
});
//链接
client.connect(options);
//订阅
client.subscribe(receive_topic, 1);
}
public void publish(String meg) throws MqttException {
MqttTopic topic = client.getTopic(send_topic);
MqttMessage message = new MqttMessage();
message.setQos(1);
message.setRetained(true);
Log.d("lhq",message.isRetained() "------ratained状态");
message.setPayload(meg.getBytes());
MqttDeliveryToken token = topic.publish(message);
token.waitForCompletion();
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论