【发布时间】:2018-07-20 00:48:44
【问题描述】:
我需要在 Ionic 3 应用程序中使用发布订阅方法。
我关注了this page。
有什么方法可以将 MQTT 与我们的 Ionic 3 应用程序链接起来?如果是,怎么会? 我究竟需要怎样做才能成功连接?
我安装了ng2-mqtt 服务使用
npm install ng2-mqtt --save
这是我的代码:
index.html
<script src="cordova.js"></script>
<script src="node_modules/ng2-mqtt/mqttws31.js" type="text/javascript"></script>
home.ts
import {Paho} from 'mqttws31'
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
private _client: Paho.MQTT.Client;
constructor(public paho: Paho) {
}
this._client = new Paho.MQTT.Client("52.66.30.178", 1883, "path", "someclient_id");
this._client.onConnectionLost = (responseObject: Object) => {
console.log('Connection lost.');
this.getServerMessage();
this._client.onMessageArrived = (message: Paho.MQTT.Message) => {
console.log('Message arrived.');
};
this._client.connect({ onSuccess: this.onConnected.bind(this); });
}
我还是不能让它工作。
任何建议和更改都会对我有所帮助。我被卡住了,请这样做。
【问题讨论】:
标签: javascript angular typescript ionic3 mqtt