前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >解决微信小程序MQTT通讯真机调试失败的问题附加可用代码

解决微信小程序MQTT通讯真机调试失败的问题附加可用代码

作者头像
德宏大魔王
发布2023-08-08 14:05:29
7480
发布2023-08-08 14:05:29
举报
文章被收录于专栏:cloud stdio

原因:模拟器上测试可以,选中了不校验合法域名,真机调试没能连接服务器,解决思路换了个mqtt.js 可参考

https://unpkg.com/mqtt@2.18.8/dist/mqtt.min.js

废话不多说!上代码 前端部分自己改改就可以用了,我懒得改了 微信小程序index.wxml

代码语言:javascript
复制
<view style="width: 750rpx;height:330rpx;">  
<image src="https://img-blog.csdnimg.cn/295062b06acc4f94b73cae854ee15d5f.png" style="width: 750rpx;height:330rpx;"></image>
</view>

<view style="width: 750rpx;height:800rpx;display: flex;flex-direction: column;justify-content: center;justify-items: center;background-color: royalblue;">
 
<!-- 开关 -->
<view style="background-color: salmon;width: 750rpx;height: 400rpx;display: flex;flex-direction: row;padding-top: 10rpx;">
<button type="warn" style="width: 180rpx;height: 80rpx;" bindtap="public" data-id="1">打开</button>
<button type="warn" style="width: 180rpx;height: 80rpx;" bindtap="public" data-id="0">关闭</button>
</view>
<!-- 状态 -->
<view style="background-color: rgb(216, 184, 181);width: 750rpx;height: 400rpx;display: flex;flex-direction: column;font-size: larger;color: seashell;font-weight: 1000;">
<text>状态:{{state}}</text>
<text>位置:</text>
</view>
</view>

index.js

代码语言:javascript
复制
// index.js
import mqtt from '../../utils/mqtt.js'    //引入mqtt
// import mqtt from '../../utils/mqtt.js';
let client = null;
// 获取应用实例
const app = getApp()

Page({
	data: {
		state:'未连接..' 
  },

	
public:function(ee){
//消息发送
	console.log(ee.currentTarget.dataset.id);
// 发布消息
client.publish('pop', ee.currentTarget.dataset.id);

wx.showToast({
	title: '请求成功',
	icon:'none'
})
},
connectMqtt: function() {
		
		var clinet_id = parseInt(Math.random() * 100 + 888888888, 10);
		console.log('wx_' + clinet_id);
		const options = {
				connectTimeout: 4000, // 超时时间
				clientId: 'wx_' + clinet_id,
				username: '账号',
				password: '密码',
		}

		client = mqtt.connect('wx://你的ip:8083/mqtt', options)
		console.log(client);
		client.on('reconnect', (error) => {
				console.log('正在重连:', error)
		})

		client.on('error', (error) => {
				console.log('连接失败:', error)
		})

		let that = this;
		client.on('connect', (e) => {
				console.log('成功连接服务器')
       //订阅一个主题
				client.subscribe('pop', {
						qos: 0
				}, function(err) {
						if (!err) {
								console.log("订阅成功")
								that.setData({
									state:'已连接pop服务器'
								})
							
						}
				})
		})
		
		client.on('message', function (topic, message) {
				console.log('received msg:' + message.toString());
				// wx.showToast({
				// 	title: message.toString(),
				// })
		})
},

onLoad:function(){
	let that=this;
	that.connectMqtt();
	
},

		
})

无法在真机上使用的请换一下MQTT的js

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档