我正在尝试连接到我的MQTT代理,我已经在我的ionic 2,angular 2应用程序中创建了一个mqtt提供程序,该提供程序如下所示:
import { Component } from '@angular/core';
import { NavController, ViewController } from 'ionic-angular';
import { Observable } from 'rxjs/Observable';
import { Paho } from 'ng2-mqtt/mqttws31';
@Component({
selector: 'page-greywater',
templateUrl: 'greywater.html'
})
export class MQTT_Greenchain {
private _client: Paho.MQTT.Client;
private options = {
userName: 'rdjghvoh',
password: 'w7Ex0VTqZViw',
timeout: 30,
useSSL:true,
onSuccess:this.onConnected,
};
private topic: string;
public displayedMessage: string;
public mes: Paho.MQTT.Message;
public constructor() {
this._client = new Paho.MQTT.Client(
"m20.cloudmqtt.com",
Number(30775),
"",
"peter"
);
this._client.onConnectionLost = (responseObject: {errorCode: Number, errorMessage: string}) => {
console.log('poes');
console.log(responseObject.errorMessage);
};
this._client.onMessageArrived = (message: Paho.MQTT.Message) => {
this.onMessageArr(message);
console.log('Message arrived.');
};
this.topic = "haha";
this.displayedMessage = "what I was";
}
connectMe() {
console.log("MQTT OPTIONS: " + this.options);
this._client.connect(this.options);
}
private onConnected(): void {
console.log('Connected to broker.');
this._client.subscribe(this.topic);
this.mes = new Paho.MQTT.Message("-1"); // -1 => Notify
this.mes.destinationName = this.topic;
this._client.send(this.mes);
}
private onMessageArr(message: Paho.MQTT.Message){
this.displayedMessage = message.payloadString;
}
}我已经能够毫无困难地在angular 1中调用下面的代码,并且我能够让所有与MQTT相关的东西都正常工作。角度1中的函数如下:
function onConnect() {
sharedUtils.hideLoading();
console.log("onConnect, CURRENT TOPIC: " + mqttData);
client.subscribe(mqttData.currentTopic);
}在上面的代码中,mqttData.currentTopic仅仅是一个字符串。
该函数接受1个参数,即使它可以接受2个( options对象)。
在angular 2中,typescript给了我一个错误:
Supplied parameters do not match any signature of call target为什么它不允许我像在angular 1中那样用一个参数调用函数?如果我给它{}作为第二个参数:
this._client.subscribe(this.topic, {});我得到的错误是:
AMQJS0005E Internal error. Error Message: Cannot read property 'subscribe' of undefined, Stack trace: TypeError: Cannot read property 'subscribe' of undefined这是在传递给onConnectionLost回调函数的response对象参数中收到的错误。
我非常确定我的'this._client‘不是未定义的,因为消息是’已连接到代理‘。出现在控制台中,表明明显调用了connect方法的onConnected、onSuccess属性回调?
我有什么没听懂的?
发布于 2018-12-04 23:47:28
试一试,如果工作正常,请回复
this.client.subscribe(this.topic, '');https://stackoverflow.com/questions/42723018
复制相似问题