android使用paho mqtt报错不可识别的包

卧龙岗唱秦腔 发布于 02/20 11:56
阅读 31
收藏 0

开春大礼《华为云技术精选集》大厂100+前沿技术实战分享!>>>

android 使用paho mqtt连接activeMQ,报错不可识别的包,跟踪代码发现错误出现在CommsReceiver中

receiving = in.available() > 0;
MqttWireMessage message = in.readMqttWireMessage();
/**
 * Reads an <code>MqttWireMessage</code> from the stream.
 */
public MqttWireMessage readMqttWireMessage() throws IOException, MqttException {
   final String methodName ="readMqttWireMessage";
   ByteArrayOutputStream bais = new ByteArrayOutputStream();
   byte first = in.readByte();
   clientState.notifyReceivedBytes(1);
   
   byte type = (byte) ((first >>> 4) & 0x0F);
   if ((type < MqttWireMessage.MESSAGE_TYPE_CONNECT) ||
      (type > MqttWireMessage.MESSAGE_TYPE_DISCONNECT)) {
      // Invalid MQTT message type...
      throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_INVALID_MESSAGE);
   }
   long remLen = MqttWireMessage.readMBI(in).getValue();
   bais.write(first);
   // bit silly, we decode it then encode it
   bais.write(MqttWireMessage.encodeMBI(remLen));
   byte[] packet = new byte[(int)(bais.size()+remLen)];
   readFully(packet,bais.size(),packet.length - bais.size());
   
   byte[] header = bais.toByteArray();
   System.arraycopy(header,0,packet,0, header.length);
   MqttWireMessage message = MqttWireMessage.createWireMessage(packet);
   // @TRACE 501= received {0} 
   log.fine(CLASS_NAME, methodName, "501",new Object[] {message});
   return message;
}

这一步中first = 0进入了if,查看了上一步源码发现是CommsReceiver新建过程中传入的inputStream

// Connect to the server at the network level e.g. TCP socket and then
// start the background processing threads before sending the connect
// packet.
NetworkModule networkModule = networkModules[networkModuleIndex];
networkModule.start();
receiver = new CommsReceiver(clientComms, clientState, tokenStore, networkModule.getInputStream());

这里打了断点看到networkModule的内容

 

这是否表示socket连接失败,所以无法读取到信息,first就等于0了?还是其他的原因?

以下还有first、type和in的内容

加载中
0
卧龙岗唱秦腔
卧龙岗唱秦腔

已解决,是后台未开放1883端口导致mqtt连接不上

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部