赞
踩
- 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())
-
- })

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。