async connectMqtt() {
return new Promise((resolve, reject) => {
const options = {
connectTimeout: 5000,
clientId: this.mqttInfo.clientAccessInfo.clientId,
address: this.mqttInfo.clientAccessInfo.endPoint, // 输入的连接地址
port: 443, // 输入的端口号
username: this.mqttInfo.clientAccessInfo.username, // 输入的用户名
password: this.mqttInfo.clientAccessInfo.password, // 输入的密码
resubscribe: true,
keepalive: 20
}
this.client = mqtt.connect('wxs://' + options.address, options) // 连接方法
this.client.on('connect', (res: any) => {
console.warn('连接成功')
resolve(res)
})
this.client.on('reconnect', (error: any) => {
console.warn('正在重连:', error)
})
this.client.on('close', (error: any) => {
console.warn('断开连接', error)
})
this.client.on('error', async (error: any) => {
console.warn('连接失败:', error)
reject()
})
})
},
0 个回复