微信小程序中使用mqtt(拿来即用版)

var options = {

//mqtt客户端的id,这里面应该还可以加上其他参数,具体看官方文档

clientId: 'mqttjs_' + (Math.random() * 1000000).toString(),

username: "jpIegqwg",

password: "rrwidm"

}

console.log(options.clientId);

//浏览器采用websocket协议,host主机地址为192.168.0.200,端口为9001,路径为/mqtt

var client = mqtt.connect("ws://cli3.mq.dwinhmi.com.cn:22884", options) // you add a ws:// url here

//建立连接

client.on('connect', function() {

    console.log("connect success")

//订阅主题 presence

client.subscribe('/dw/1/rx/1615/30/901/3c6105034fe8/1_10048_Aijruiqu/json', function(err) {

console.log(err)

if (!err) {

console.log("subscribe success!")

} else {

//打印错误

console.log(err)

}

})

// 发送消息

client.publish('/dw/1/rx/1615/30/901/3c6105034fe8/1_10048_Aijruiqu/json', '{"water_status": "","__command_type": "READ"}')

})

//如果连接错误,打印错误

client.on('error', function(err) {

console.log(err)

client.end()

})

//如果client订阅主题成功,那么这里就是当接收到自己订阅主题的处理逻辑

client.on('message', function(topic, message) {

// message is Buffer,此处就是打印消息的具体内容

console.log('-> ' + message.toString())

})

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值