标签:login str div use sub min mqtt qos 连接
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>mqtt</title> <p>官方文档参考网址:https://docs.emqx.io/tutorial/v3/cn/client_dev/javascript.html</p> <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script> <script> const TCP_URL = ‘ws://192.168.1.54:8083/mqtt‘ const options = { // 超时时间 connectTimeout: 4000, clientId: ‘test1‘, // 心跳时间 keepalive: 30, clean: false, } const client = mqtt.connect(TCP_URL, options) // 判断是否已成功连接 if (!client.connected) { console.log(‘客户端连接成功。。。。。。。‘) } var payload = new Object();//传递信息的对象 payload.userName ="wjf3"; payload.password = "123456"; payload.securityCode = "123456789"; //1.订阅主题动作 //client.subscribe(‘/#‘, { qos: 2 }, (error) => { // if (!error) { // console.log(‘订阅成功‘) // } //}) //2.发布动作 //client.publish(‘/mo/wftdlx/login‘, JSON.stringify(payload),{ qos: 2, rein: false }, (err) => { // console.log(err || ‘发布成功‘) //}) //3.取消订阅动作 //client.unsubscribe(‘/#‘, // (err) => { // console.log(err || ‘取消订阅成功‘) // },) //4.手动断开连接动作 // client.end() //监控程序================================================= //1.【clientId】接收【所有订阅主题】的消息 client.on(‘message‘, (topic, message) => { console.log(‘收到来自‘, topic, ‘的消息:‘, message.toString()) }) //2.成功连接 // client.on(‘connect‘, () => { //console.log(‘成功连接‘, TCP_URL) //}) //3.连接错误 client.on(‘error‘, (error) => { console.log(‘连接失败:‘, error) //4.重连操作 client.on(‘reconnect‘, (error) => { console.log(‘正在重连:‘, error) }) }) </script> </head> <body> </body> </html>
标签:login str div use sub min mqtt qos 连接
原文地址:https://www.cnblogs.com/ynhk/p/11840136.html