如何解决尝试围绕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
在连接选项中传递一个对象,以用作onFailure
和invocationContext
回调的上下文,但不能传递其他回调。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。