zoukankan      html  css  js  c++  java
  • angular + mqtt

    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.

    Install it using npm install ngx-mqtt --save

    Usage: 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)

    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});
     }
  • 相关阅读:
    Java之this关键字的用法
    JavaSE 之 final 初探
    LinkedList 浅析示例
    HashSet 浅析示例
    ArrayList 浅析示例
    MySQL5.7 修改密码
    IE10 和 Chrome50 对日期 new Date() 支持的区别
    databtables 设置(显示)行号
    团队管理
    财务名称
  • 原文地址:https://www.cnblogs.com/adjk/p/10272669.html
Copyright © 2011-2022 走看看