ICHPublishClient.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.emato.ich.message;
  2. import android.util.Log;
  3. import com.emato.ich.contant.ICSPConstant;
  4. import com.emato.ich.utils.BaseUtils;
  5. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  6. import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
  7. import org.eclipse.paho.client.mqttv3.MqttCallback;
  8. import org.eclipse.paho.client.mqttv3.MqttClient;
  9. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  10. import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
  11. import org.eclipse.paho.client.mqttv3.MqttException;
  12. import org.eclipse.paho.client.mqttv3.MqttMessage;
  13. import org.eclipse.paho.client.mqttv3.MqttTopic;
  14. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
  15. /**
  16. * 发布客户端
  17. */
  18. public class ICHPublishClient {
  19. public static final String TAG = "IchClient";
  20. public static final String userName = "admin";
  21. public static final String password = "public";
  22. private MqttClient client;// 客户端实例
  23. private MqttTopic topic;// 主题实例
  24. private MqttMessage message;// 消息
  25. public static ICHPublishClient getInstance(){
  26. return ICHPublishClientInnerClass.ichPublishClient;
  27. }
  28. static class ICHPublishClientInnerClass{
  29. public static ICHPublishClient ichPublishClient = new ICHPublishClient();
  30. }
  31. // 初始化客户端实例
  32. private ICHPublishClient() {
  33. // String clientId = BaseUtils.getClientId() + "publish";
  34. String clientId = BaseUtils.getClientId();
  35. try {
  36. Log.i(TAG, "ICHPublishClient: 创建客户端实例开始!");
  37. client = new MqttClient(ICSPConstant.MQTT_SERVER_ADDRESS, clientId, new MemoryPersistence());
  38. Log.i(TAG, "ICHPublishClient: 创建客户端实例完成!");
  39. } catch (MqttException e) {
  40. Log.e(TAG, "ICHPublishClient: 创建客户端实例失败!", e);
  41. }
  42. connect();
  43. }
  44. // 连接服务器
  45. private void connect() {
  46. Log.i(TAG, "connect: 开始连接服务器!");
  47. MqttConnectOptions options = new MqttConnectOptions();
  48. options.setCleanSession(true);
  49. options.setUserName(userName);
  50. options.setPassword(password.toCharArray());
  51. // 连接超时时间
  52. options.setConnectionTimeout(10);
  53. // 设置心跳间隔时间
  54. options.setKeepAliveInterval(30);
  55. // TODO 设置发布回调
  56. client.setCallback(new MqttCallback() {
  57. @Override
  58. public void connectionLost(Throwable cause) {
  59. Log.e(TAG, "connectionLost: cause: ", cause);
  60. }
  61. @Override
  62. public void messageArrived(String topic, MqttMessage message) throws Exception {
  63. Log.i(TAG, "messageArrived: topic: " + topic + ", msg: " + message.getId() + "--" + new String(message.getPayload()));
  64. }
  65. @Override
  66. public void deliveryComplete(IMqttDeliveryToken token) {
  67. Log.i(TAG, "deliveryComplete: token---" + token.toString() + "---" + topic.getName());
  68. }
  69. });
  70. try {
  71. client.connect(options);
  72. Log.i(TAG, "connect: 连接消息服务器成功!");
  73. } catch (MqttException e) {
  74. Log.e(TAG, "connect: 连接消息服务器失败!", e);
  75. }
  76. }
  77. // 发布消息
  78. public void publish(String topicName, MqttMessage message) {
  79. Log.i(TAG, "publish: 发布消息, topic: " + topicName + ", message:" + message);
  80. MqttDeliveryToken token = null;
  81. try {
  82. topic = client.getTopic(topicName);
  83. token = topic.publish(message);
  84. token.waitForCompletion();
  85. Log.i(TAG, "publish: 消息发送成功 " + token.isComplete());
  86. } catch (MqttException e) {
  87. Log.e(TAG, "publish: 消息推送失败!", e);
  88. }
  89. }
  90. public void subscribe(String topicName, IMqttMessageListener iMqttMessageListener) {
  91. try {
  92. topic = client.getTopic(topicName);
  93. Log.i(TAG, "subscribe: 开始消息监听! topicName: " + topicName);
  94. client.subscribe(topicName, iMqttMessageListener);
  95. } catch (MqttException e) {
  96. Log.e(TAG, "subscribe: 消息监听失败! topicName: " + topicName, e);
  97. }
  98. }
  99. }