简体   繁体   English

与Ionic 3的MQTT连接

[英]MQTT Connection with Ionic 3

I need to use publish Subscribe methods in my Ionic 3 application. 我需要在Ionic 3应用程序中使用发布订阅方法。

I followed this page . 我关注了此页面

Is there any way we can link MQTT with our Ionic 3 application? 有什么方法可以将MQTT与Ionic 3应用程序链接? If yes, how so? 如果是,怎么办? How exactly do I need to go about it for a successful connection? 为了成功建立连接,我到底需要怎么做?

I installed ng2-mqtt service using 我使用安装了ng2-mqtt服务

npm install ng2-mqtt --save

This is my code: 这是我的代码:

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); });
  }

Still I can't get it to work. 我还是无法正常工作。

Any suggestions and changes will help me. 任何建议和更改都会对我有所帮助。 I'm stuck please do. 我被卡住了。

After searching and trying out different things for a while, I found this solution, you can use this library if you want to use MQTT in your project. 在搜索并尝试了一段时间后,我找到了这个解决方案,如果您想在项目中使用MQTT,则可以使用此库。

Install it using npm install ngx-mqtt --save 使用npm install ngx-mqtt --save安装

Usage : app.module.ts 用法 :app.module.ts

import { Observable } from 'rxjs/Observable';
import {
  IMqttMessage,
  MqttModule,
  MqttService
} from 'ngx-mqtt';

export const MQTT_SERVICE_OPTIONS = {
  hostname: '13.127.53.13',
  port: 9001,
  path: '/mqtt'
};

export function mqttServiceFactory() {
  return new MqttService(MQTT_SERVICE_OPTIONS);
}

@NgModule({
  imports: [
    BrowserModule,
    HttpModule,
    MqttModule.forRoot({
      provide: MqttService,
      useFactory: mqttServiceFactory
    }),
    IonicModule.forRoot(MyApp)
  ]

And then you can use it in your page like: (ex: home.ts file) 然后,您可以在页面中使用它,例如:(例如:home.ts文件)

import { IMqttMessage, MqttModule, MqttService } from 'ngx-mqtt';
import { Observable } from 'rxjs/Observable';

export class HomePage  {

constructor( private _mqttService: MqttService)
{
   this._mqttService.observe('home/door').subscribe((message: MqttMessage) => 
   {
   this.sensor1 = message.payload.toString();
   console.log(this.sensor1);
   });
}

 publishMessage()
 {
  this._mqttService.unsafePublish("home/button", "on", {qos: 0, retain: false});
 }

For more information about this library: https://github.com/sclausen/ngx-mqtt 有关此库的更多信息: https : //github.com/sclausen/ngx-mqtt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM