Yuworm 2024-05-26 19:53 采纳率: 20%
浏览 10
已结题

uniapp 阿里云 mqtt

uniapp使用mqtt连接阿里云,无法连接,一直重连:
我的mqtt.js代码如下:

import mqtt from 'mqtt/dist/mqtt.js' //引入mqtt依赖

var client                          
let mqttConnected = false  //mqtt连接状态,这个可以不要,直接查询client.connected即可
const publishTopic = '/k1bup6fRrSA/ESP8266_Yuworm/user/uniapp'  //发布Topic
const MQTT_IP = 'iot-06z00cg5uyb7vtn.mqtt.iothub.aliyuncs.com' //mqtt地址端口
const MQTT_OPTIONS = {
    connectTimeout: 5000,      //连接超时时间
    clientId: 'k1bup6fRrSA.uni-app|securemode=2,signmethod=hmacsha256,timestamp=1716717442073|',  //clientId不能重复,这里可以随机生成
    username: 'uni-app&k1bup6fRrSA',         //用户名
    password: 'f5965397dd9cee4cba6a851d3db8fb71c264064252cb4237d4045a367d77e7ef',         //密码
    clean: false        
}

export function tes(){
    console.log("test")
}

//创建客户端连接
export function mqttConnect() {
    // #ifdef H5
    client = mqtt.connect('ws://' + MQTT_IP, MQTT_OPTIONS,function(err){
        console.log(err)
    })
    // #endif
    // #ifdef MP-WEIXIN||APP-PLUS
    client = mqtt.connect('wx://' + MQTT_IP, MQTT_OPTIONS,function(err){
        console.log(err)
    })
    // #endif

    client.on('connect', function() {
        console.log('连接成功')
        mqttConnected = true
    }).on('reconnect', function(error) {
        console.log( '正在重连...', error)
        mqttConnected = false
    }).on('error', function(error) {
        console.log('连接失败...', error)
        mqttConnected = false
    }).on('end', function() {
        console.log('连接断开')
        mqttConnected = false
    }).on('close',function(){
        console.log('连接关闭')
        mqttConnected = false
    }).on('offline',function(){
        console.log('客户端下线')
    })
}
//发布消息
export function mqttPublish(msg){
    if(mqttConnected){
        client.publish(publishTopic,msg,{qos:1,retain:false});//hello mqtt +
        console.log('发布了一条消息',msg)
    }else{
        uni.showToast({
            title: 'MQTT服务器未连接',
            icon: 'none'
        });
    }
}
//断开连接
export function mqttDisconnect(){
    console.log('mqttConnected',mqttConnected)
    console.log('client',client)
    if(mqttConnected){
        client.end(true)
        mqttConnected = false
    }
    
}

本人考虑是不是mqtt参数或是以下地址的问题:

    client = mqtt.connect('ws://' + MQTT_IP, MQTT_OPTIONS,function(err){
        console.log(err)
    })
    // #endif
    // #ifdef MP-WEIXIN||APP-PLUS
    client = mqtt.connect('wx://' + MQTT_IP, MQTT_OPTIONS,function(err){
        console.log(err)
    })

运行截图:

img

  • 写回答

7条回答 默认 最新

  • 关注

    晚上好🌙🌙🌙
    本答案参考ChatGPT-3.5

    从你提供的代码中,我发现你的mqtt地址应该不对。你需要把MQTT_IP的值改为mqtt地址端口。例如:MQTT_IP='iot-xxxxxx.aliyuncs.com' 其中的xxxxx值为你MQTT实例的ID。

    解决方案: 1.修改MQTT_IP为正确的值。

    2.在控制台检查一下阿里云MQTT实例的相关参数设置是否正确,包括endpoint,实例ID,用户名和密码等。

    3.把ws:改为wss:,是websocket安全协议的url,具有更加安全性。

    4.在跑不起来时,可以在on('error', function(error) {}),查看底层是否有错误信息。

    修改后的代码如下所示:

    const MQTT_IP = 'iot-xxxxxxxxxx.aliyuncs.com' //mqtt地址端口
    const MQTT_OPTIONS = {
        connectTimeout: 5000,      //连接超时时间
        clientId: 'k1bup6fRrSA.uni-app|securemode=2,signmethod=hmacsha256,timestamp=1716717442073|',  //clientId不能重复,这里可以随机生成
        username: 'uni-app&k1bup6fRrSA',         //用户名
        password: 'f5965397dd9cee4cba6a851d3db8fb71c264064252cb4237d4045a367d77e7ef',         //密码
        clean: false        
    }
    
    //创建客户端连接
    export function mqttConnect() {
        // #ifdef H5
        client = mqtt.connect('wss://'+MQTT_IP+':443/mqtt', MQTT_OPTIONS,function(err){
            console.log(err)
        })
        // #endif
        // #ifdef MP-WEIXIN||APP-PLUS
        client = mqtt.connect('wxs://'+MQTT_IP+':443/mqtt', MQTT_OPTIONS,function(err){
            console.log(err)
        })
        // #endif
    
        client.on('connect', function() {
            console.log('连接成功')
            mqttConnected = true
        }).on('reconnect', function(error) {
            console.log( '正在重连...', error)
            mqttConnected = false
        }).on('error', function(error) {
            console.log('连接失败...', error)
            mqttConnected = false
        }).on('end', function() {
            console.log('连接断开')
            mqttConnected = false
        }).on('close',function(){
            console.log('连接关闭')
            mqttConnected = false
        }).on('offline',function(){
            console.log('客户端下线')
        })
    }
    

    注意:修改具体的MQTT_IP地址和相应的端口号。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 6月3日
  • 已采纳回答 5月26日
  • 修改了问题 5月26日
  • 创建了问题 5月26日

悬赏问题

  • ¥15 网络分析设施点无法识别
  • ¥15 状态图的并发态问题咨询
  • ¥15 PFC3D,plot
  • ¥15 VAE模型编程报错无法解决
  • ¥100 基于SVM的信息粒化时序回归预测,有偿求解!
  • ¥15 物体组批优化问题-数学建模求解答
  • ¥15 微信原生小程序tabBar编译报错
  • ¥350 麦克风声源定位坐标不准
  • ¥15 apifox与swagger使用
  • ¥15 egg异步请求返回404的问题