ICHPublishClient.java 4.2 KB

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