mqtt javascript客户端中的onFailure函数

时间:2017-03-23 11:32:31

标签: mqtt paho

我正在使用mqtt paho javascript客户端库连接到 mqtt服务器,我能够用用户名和密码连接到服务器,现在的问题是......如果用户输入错误的用户名或密码,我该如何警告用户。i followed this link 如果用户凭据错误,是否有任何onFailure函数报告错误?



client.connect({onSuccess:this.onConnect.bind(this),userName:Name,password:Password},{onFailure:console.log("failed")});



  onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  
  }




  

即使我能够连接,我也可以看到" onConnect"并且"失败"我的控制台中的消息

3 个答案:

答案 0 :(得分:0)

您需要阅读经纪商带给您的 连接返回代码 字段

协议中定义了一系列常量:

您正在寻找的是:

  

0x04连接被拒绝,用户名或密码错误

指定的其他值是:

  

0x00:接受连接

     

0x01:拒绝连接

     

0x02:拒绝连接

     

0x03:拒绝连接

     

0x04:拒绝连接

     

0x05:连接被拒绝,未经授权

     

从6-255:保留供将来使用

答案 1 :(得分:0)

取自文档here

Paho connect方法接受一个options参数,其中包含onFailure回调函数,该函数将包含身份验证失败的错误代码:

client.connect({
  onFailure: function(err) {
    if (err.errorCode == 0x4) {
      //bad user/password
    }
  }
});

答案 2 :(得分:0)

connect方法将connectionOptions作为参数,因此,您可以提供onSuccess,onFailure,userName,password等选项。将单个响应对象参数传递给onFailure回调,该回调具有errorCode等字段 ,错误信息。



SUBPROJECT_CONFIG




相关问题