angular + mqtt
发布时间:2020-12-17 07:02:53 所属栏目:安全 来源:网络整理
导读: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 }
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,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}); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Scala sbt run – “不支持的major.minor版本52.0”
- 访问webservice异常:因 URL 意外地以 结束,请求格式无法识
- 如何从Scala Spark中的Excel(xls,xlsx)文件构造Dataframe?
- typescript – Angular2 ag-grid数据源
- scala – 如何从Download Spark页面的源代码构建Spark?
- REST another WebService???
- ssm框架整合webservice
- "Unable to find record. No key specified"错误分
- angularjs – 如何防止$watch内部的价值变化
- bash – “grep -c”与“wc -l”