我通常在python中编程,我正在尝试通常在python中运行的东西。我在课堂上有一个功能。它是mqtt库的回调函数。我想要的是将我收到的消息(在函数onMessageArrived中)保存到名为(this。)buffer的类变量中。
class sub{
constructor(hostname,port,clientid,topic){
this.buffer = [];
this.hostname=hostname;
this.port=port;
this.clientid = clientid;
this.topic = topic;
this.client = new Paho.MQTT.Client(hostname,port, clientid);
// set callback handlers
this.client.onConnectionLost = this.onConnectionLost;
this.client.onMessageArrived = this.onMessageArrived; <----Problem
// connect the client
this.client.connect({onSuccess:this.onConnect});
}
onConnect(){
console.log('OnConnect');
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
this.buffer.push(message.payloadString) <-------------Problem
}
subs(){
this.client.subscribe(this.topic)
}
publ(message){
var mg = new Paho.MQTT.Message(message);
mg.destinationName = this.topic;
this.client.send(mg);
}
}
问题是onMessageArrived函数不会将消息推送到this.buffer变量中 - 看起来函数不知道它的内部类,因此我无法通过&#39;这来访问类变量。&#39;。在python中它以这种方式工作。我非常绝望,因为我看起来像是这个功能被完全隔离,除了打印信息之外别无他法。
the error:
onConnectionLost:AMQJS0005E Internal error. Error Message: Cannot read
property 'push' of undefined, Stack trace: TypeError: Cannot read property
'push' of undefined
提前致谢