IONIC-MQTT 插件在 MessageArrived | 之后不调用任何方法Angular 8 + 离子
IONIC-MQTT plugin doesn't call any method after MessageArrived | Angular 8 + ionic
大家好,我需要帮助,
所以:我已经安装了这个:https://www.npmjs.com/package/ionic-mqtt
所以这是我的代码:
public TOPIC: string[] = ['Tagliavini/OvenStatus/#'];
public MQTT_CONFIG: {
host: string,
port: number,
clientId: string,
} = {
// host: "mqtt.micro-systems.it",
// port: 9001,
// clientId: "mqtt",
host: "192.168.0.171",
port: 9001,
clientId: "mqtt",
};
ngOnInit() {
this._mqttClient = this.mqttService.loadingMqtt(this.ConnectionLost, this.MessageArrived, this.TOPIC, this.MQTT_CONFIG);
}
MessageArrived(message) {
console.log(message.payloadString) // this print the correct message
this.elaborateMessageArrived(message); // this got error when call
}
elaborateMessageArrived(message){
console.log("try to call me");
}
所以,连接工作得更好,但是当我从 mqtt 代理接收有效负载时,控制台日志在控制台上打印正确消息,就像你看到的那样,但是当我调用
this.elaborareMessageArrived(message)
错误是:
{errorCode: 5, errorMessage: "AMQJS0005E Internal error. Error
Message: this.ela…a function, Stack trace: No Error Stack Available",
reconnect: undefined, uri: "ws://192.168.0.171:9001/mqtt"} errorCode:
5 errorMessage: "AMQJS0005E Internal error. Error Message:
this.elaborateMessageArrived is not a function, Stack trace: No Error
Stack Available" reconnect: undefined uri:
"ws://192.168.0.171:9001/mqtt"
proto: Object
这是不可能的,该函数存在,并且可以与其他方法一起使用,但是当我直接在 MessageArrived 中调用时,我得到了这个错误,我该如何解决?
谢谢
尝试像这样在 class 上下文中绑定函数:
ngOnInit() {
this._mqttClient = this.mqttService.loadingMqtt(this.ConnectionLost, this.MessageArrived.bind(this), this.TOPIC, this.MQTT_CONFIG);
}`
这应该可以解决您的问题。
bind(this)
的一些文档在此处:
The simplest use of bind() is to make a function that, no matter how
it is called, is called with a particular this value. A common mistake
for new JavaScript programmers is to extract a method from an object,
then to later call that function and expect it to use the original
object as its this (e.g. by using that method in callback-based code).
Without special care, however, the original object is usually lost.
大家好,我需要帮助, 所以:我已经安装了这个:https://www.npmjs.com/package/ionic-mqtt
所以这是我的代码:
public TOPIC: string[] = ['Tagliavini/OvenStatus/#'];
public MQTT_CONFIG: {
host: string,
port: number,
clientId: string,
} = {
// host: "mqtt.micro-systems.it",
// port: 9001,
// clientId: "mqtt",
host: "192.168.0.171",
port: 9001,
clientId: "mqtt",
};
ngOnInit() {
this._mqttClient = this.mqttService.loadingMqtt(this.ConnectionLost, this.MessageArrived, this.TOPIC, this.MQTT_CONFIG);
}
MessageArrived(message) {
console.log(message.payloadString) // this print the correct message
this.elaborateMessageArrived(message); // this got error when call
}
elaborateMessageArrived(message){
console.log("try to call me");
}
所以,连接工作得更好,但是当我从 mqtt 代理接收有效负载时,控制台日志在控制台上打印正确消息,就像你看到的那样,但是当我调用
this.elaborareMessageArrived(message)
错误是:
{errorCode: 5, errorMessage: "AMQJS0005E Internal error. Error Message: this.ela…a function, Stack trace: No Error Stack Available", reconnect: undefined, uri: "ws://192.168.0.171:9001/mqtt"} errorCode: 5 errorMessage: "AMQJS0005E Internal error. Error Message: this.elaborateMessageArrived is not a function, Stack trace: No Error Stack Available" reconnect: undefined uri: "ws://192.168.0.171:9001/mqtt" proto: Object
这是不可能的,该函数存在,并且可以与其他方法一起使用,但是当我直接在 MessageArrived 中调用时,我得到了这个错误,我该如何解决?
谢谢
尝试像这样在 class 上下文中绑定函数:
ngOnInit() {
this._mqttClient = this.mqttService.loadingMqtt(this.ConnectionLost, this.MessageArrived.bind(this), this.TOPIC, this.MQTT_CONFIG);
}`
这应该可以解决您的问题。
bind(this)
的一些文档在此处:
The simplest use of bind() is to make a function that, no matter how it is called, is called with a particular this value. A common mistake for new JavaScript programmers is to extract a method from an object, then to later call that function and expect it to use the original object as its this (e.g. by using that method in callback-based code). Without special care, however, the original object is usually lost.