我需要在我的 Ionic 3 应用程序中使用发布订阅方法。
我关注了这个页面。
有什么方法可以将 MQTT 与我们的 Ionic 3 应用程序联系起来?如果是,怎么会?我究竟需要怎么做才能成功连接?
我ng2-mqtt使用安装服务
npm install ng2-mqtt --save
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
index.html
<script src="cordova.js"></script>
<script src="node_modules/ng2-mqtt/mqttws31.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
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); });
} …Run Code Online (Sandbox Code Playgroud)