我使用Mosquitto库创建了MQTT代理。我创建了1个Angular 5 Ionic 3移动应用程序,用作paho客户。我能够启动mqtt经纪人。当我在Android设备上运行移动应用程序时。它尝试连接到代理,并且出现“客户端上的套接字错误,正在断开连接”。立即。
我想知道如何解决此问题。 我也想知道如何在Mac上创建Paho经纪人
Musquitto代理:已按照以下步骤在mac OSX上安装和运行代理
/usr/bin/ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew
/install/master/install)"
brew install mosquitto
/usr/local/sbin/mosquitto -c
/usr/local/etc/mosquitto/mosquitto.conf
这是我从终端收到的回复
1548310503: mosquitto version 1.5.1 starting
1548310503: Config loaded from /usr/local/etc/mosquitto/mosquitto.conf.
1548310503: Opening ipv6 listen socket on port 1883.
1548310503: Opening ipv4 listen socket on port 1883
现在这是有角度的5 ionic 3应用程序代码
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {Paho} from 'ng2-mqtt/mqttws31';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
client;
constructor(public navCtrl: NavController) {
// Anikets LG Phone Client ID: 123456 aniketPhone
// Pixel Client ID qwerty12345 pixelPhone
this.client = new Paho.MQTT.Client('192.168.225.58', 1883,
'aniketPhone');
this.onMessage();
this.onConnectionLost();
this.client.connect({onSuccess: this.onConnected.bind(this)});
}
onConnected() {
console.log("Connected");
this.client.subscribe("pixelPhone");
this.sendMessage('HelloWorld');
}
sendMessage(message: string) {
let packet = new Paho.MQTT.Message(message);
packet.destinationName = "pixelPhone";
this.client.send(packet);
}
onMessage() {
this.client.onMessageArrived = (message: Paho.MQTT.Message) => {
console.log('Message arrived : ' + message.payloadString);
alert(message.payloadString)
};
}
onConnectionLost() {
this.client.onConnectionLost = (responseObject: Object) => {
console.log('Connection lost : ' + JSON.stringify(responseObject));
};
}
}
答案 0 :(得分:1)
Paho Javascript客户端仅通过Websocket支持MQTT。
Mosquitto默认只监听本地MQTT(在端口1883上),如果要使用Websocket客户端,则需要添加一个额外的监听器。
将以下内容添加到mosquitto.conf文件中:
<Router>
<Container>
<Header/> // it is intended
<Switch>
<Route exact path='/' component={ Home } />
<Route path='/news/:category/:id/:title' component={ SingleArticle } />
<Route path='/news' component={ Home } />
<Route path='/live' component={ Live } />
<Route path='/admin' component={ AdminPanel } /> //here I want all my admin routes which generates its own header and footer
<Route path='*' component={ NotFound } />
</Switch>
<Footer /> // it is intended
</Container>
</Router>
您需要在角度代码中更改端口号。
您在代码中也有一个硬编码的clientId,由于每个客户端都需要一个唯一的clientId,因此一次只能连接1个客户端。