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

Trying to write a wrapper class around Paho MQTT Javascript client

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

我对 JavaScript classes 不是很满意,而且我在试图找出问题所在时遇到了麻烦...

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 上,我可以 publish/subscribe 使用 class 之外的 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);
  }
}

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

解法:

我需要传递一个对象(在本例中是 Hermes class 的实例)以用作 onSuccess 回调的上下文,而不是使用 this(这不是我想的那样,像往常一样...),在连接选项中使用 invocationContext

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,
      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", "test", handleMessage);

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

回调全部来自客户端网络处理程序,因此 this 实际上是对处理程序的引用。

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