尝试围绕Paho MQTT Javascript客户端编写包装器类

问题描述

我正在尝试围绕Paho MQTT JavaScript客户端编写一个简单的包装器类。 (该想法是围绕MQTT消息传递进行一些额外的验证,以确保按照正确的顺序处理消息。)

我对JavaScript类不太满意,并且在弄乱这是怎么回事的过程中一团糟...

class Hermes {
  constructor(uri,topic,callback) {
    var clientId = "clientID_" + parseInt(Math.random() * 1000);
    this.client = new Paho.MQTT.Client(uri,clientId);
    this.topic = topic;
    this.callback = callback;
    this.client.onMessageArrived = this._onMessageArrived;
    this.client.onConnectionLost = this._onConnectionLost;
    this.client.connect({
      onSuccess: this._onConnect,onFailure: this._onFailure
    });
  }
  _onConnect() {
    // Once a connection has been made,make a subscription and send a message.
    console.log("_onConnect: " + this.client.clientId)
    this.client.subscribe(this.topic);
  }
  // called when connection fails
  _onFailure(responSEObject) {
    console.log("_onFailure: "+responSEObject.errorMessage);
  }
  // called when a message arrives
  _onMessageArrived(message) {
    console.log("_onMessageArrived: "+message.payloadString)
    // Todo: validate message and pass to callback
  }
  // called when client loses connection
  _onConnectionLost(responSEObject) {
    if (responSEObject.errorCode !== 0) {
      console.log("onConnectionLost: "+responSEObject.errorMessage);
    }
  }
}


function handleMessage(message) {
// Todo: handle message
}
var hermes = new Hermes("ws://mqtt.example.com:9001/mqtt","test",handleMessage);

预期结果: 客户端成功连接后,_onConnect: clientID_xxx应该记录在控制台中。

实际结果:

onConnectionLost: AMQJS0005E Internal error. Error Message: undefined is not an object (evaluating 'this.client.clientId'),Stack trace: _onConnect@file:///Users/richardguy/Desktop/hermes.js:16:45

MQTT代理正在VPS上运行,我可以使用类外的Paho Javascript库成功发布/订阅消息,像这样...

uri = "ws://mqtt.example.com:9001/mqtt"
var clientId = "clientID_" + parseInt(Math.random() * 1000);
client = new Paho.MQTT.Client(uri,clientId);
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({
  onSuccess: onConnect,onFailure: onFailure
});
function onConnect() {
  // Once a connection has been made,make a subscription and send a message.
  console.log("_onConnect: " + client.clientId)
  client.subscribe("test");
}
// called when connection fails
function onFailure(responSEObject) {
  console.log("_onFailure: "+responSEObject.errorMessage);
}
// called when a message arrives
function onMessageArrived(message) {
  console.log("_onMessageArrived: "+message.payloadString)
  // Todo: validate message and pass to callback
}
// called when client loses connection
function onConnectionLost(responSEObject) {
  if (responSEObject.errorCode !== 0) {
    console.log("onConnectionLost: "+responSEObject.errorMessage);
  }
}

这仅仅是类定义中的错误,还是与Paho MQTT库有关?

解决方案:

我需要传递一个对象(在这种情况下为Hermes类的实例)用作onSuccess回调的上下文,而不是使用this(这不是我原本以为是...),在连接选项中使用invocationContext

class Hermes {
  constructor(uri,onFailure: this._onFailure,invocationContext: this
    });
  }
  _onConnect(responSEObject) {
    // Once a connection has been made,make a subscription and send a message.
    let self = responSEObject.invocationContext;
    self.client.subscribe(self.topic);
  }
  // called when connection fails
  _onFailure(responSEObject) {
    console.log("_onFailure: "+responSEObject.errorMessage);
  }
  // called when a message arrives
  _onMessageArrived(message) {
    console.log("_onMessageArrived: "+message.payloadString)
    // Todo: validate message and pass to callback
  }
  // called when client loses connection
  _onConnectionLost(responSEObject) {
    if (responSEObject.errorCode !== 0) {
      console.log("onConnectionLost: "+responSEObject.errorMessage);
    }
  }
}


function handleMessage(message) {
}
var hermes = new Hermes("ws://mqtt.example.com:8080/mqtt",handleMessage);

解决方法

您的问题是this不是您想的那样。

所有回调都是由客户端网络处理程序进行的,因此this实际上是对处理程序的引用。

您可以使用onSuccess在连接选项中传递一个对象,以用作onFailureinvocationContext回调的上下文,但不能传递其他回调。