MQTT仅在一台设备上工作

时间:2020-01-22 12:24:59

标签: javascript ionic-framework mqtt mosquitto broker

我制作了一个通过Websocket发布的Ionic PWA mqtt,但是当我部署该应用程序(我正在使用AWS)时,它只能在Google Chrome浏览器中运行,如果我在隐身窗口中对其进行测试,则不会。工作。

在控制台中,它在所有无法运行的设备(电脑/移动设备)上显示此错误:

bSocket与'ws://test.mosquitto.org:8081 /'的连接失败:WebSocket握手期间出错:net :: ERR_CONNECTION_RESET

下面是我的JavaScript离子代码

import { Component } from '@angular/core';
import { MQTTService } from 'ionic-mqtt';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

  private _mqttClient: any;
  temp = 19;

  private MQTT_CONFIG: {
    host: string,
    port: number,
    clientId: string,
    path: string,
  } = {
    host: "test.mosquitto.org",
    port: 8081,
    clientId: "qeekljeqe" + Math.floor(Math.random()*100),
    path: "/",
  };

  private TOPIC: string[] = [];

  constructor(private _mqttService: MQTTService) {
  }

  ngOnInit() {
    this._mqttClient = this._mqttService.loadingMqtt(this._onConnectionLost, this._onMessageArrived, this.TOPIC, this.MQTT_CONFIG);
  }

  private _onConnectionLost(responseObject) {
    // connection listener
    // ...do actions when connection lost
    console.log('_onConnectionLost', responseObject);
  }

  private _onMessageArrived(message) {
    // message listener
    // ...do actions with arriving message
    console.log('message', message);  
}
public publishMessage(TOPICO : string, VALOR : string) {
  console.log('publishMessage')
  this._mqttService.publishMessage(TOPICO, VALOR);
}

0 个答案:

没有答案