mqtt发起连接后没反应是什么原因?

用的包 org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.4

帮我看看下面这段代码,极大概率不发起mqtt连接和不触发回调。是什么原因?如何解决?

new Thread(() -> {
  // 建立 mqtt 连接
  CompletableFuture<HttpUtil.Response> settingFuture = HttpUtil.get(HttpUtil.url(AppConfig.apiUrl, SettingsApi.dataApi), null, null);
  CompletableFuture<String> deviceIdFuture = AppCommon.getDeviceIdAsync(context);
  HttpUtil.Response settingsRes;
  String deviceId;
  try {
      // 阻塞 - 直到所有完成
      CompletableFuture.allOf(settingFuture, deviceIdFuture).get();
      settingsRes = settingFuture.get();
      deviceId = deviceIdFuture.get();
  } catch (Exception e) {
      e.printStackTrace();
      LogUtil.debug("mqtt 连接发生异常", e.getMessage());
      return ;
  }
  this.client = new MqttAndroidClient(context, url, deviceId);
  this.client.setCallback(new MqttCallback() {
      @Override
      public void connectionLost(Throwable cause) {
          if (cause != null) {
              cause.printStackTrace();
          }
          LogUtil.debug("mqtt-client lost");
      }

      @Override
      public void messageArrived(String topic, MqttMessage message) {
          try {
              String messageString = message.toString();
              LogUtil.debug("mqtt-client onmessage", topic, messageString);
          } catch (Exception e) {
              e.printStackTrace();
              LogUtil.debug("mqtt onmessage excep", e.getMessage());
          }
      }

      @Override
      public void deliveryComplete(IMqttDeliveryToken token) {
          LogUtil.debug("mqtt-client 发送成功");
      }
  });
}).start();

日志如下:

当前线程[Thread-3]: 初始化 mqtt 连接
当前线程[main]: ["mqtt device_id async","3056b1e3-xxxx"]
当前线程[main]: ["settings",{"android_apk_version":"v1.0.0","contact_email":"","education_l":"" ... 此处省略很多}
当前线程[Thread-3]: ["mqtt","url","ssl://127.0.0.1:8883","deviceId","3056b1e3-xxxx","username","test","password","test"]
当前线程[main]: mqtt 已发起连接
阅读 727
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏