簡體   English   中英

嘗試圍繞 Paho MQTT Javascript 客戶端編寫包裝類

[英]Trying to write a wrapper class around Paho MQTT Javascript client

我正在嘗試圍繞 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, 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回調的上下文,但不能用於其他回調。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM