1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script> <script> // 将在全局初始化一个 mqtt 变量 //console.log(mqtt) // 连接选项 const options = { connectTimeout: 4000, // 超时时间 // 认证信息 clientId: 'emqx-connect-via-webstest', //username: '', //password: '', }
//const client = mqtt.connect('ws://cluee.tech:8083/mqtt', options) const client = mqtt.connect('wss://cluee.tech:18084/mqtt', options)
client.on('connect', (e) => { console.log('成功连接服务器') // 订阅一个主题 client.subscribe('hello', { qos: 1 }, (error) => { if (!error) { console.log('订阅成功'); client.publish('hello', 'Hello EMQ', { qos: 1, rein: false }, (error) => { console.log(error || '发布成功') }) } }) }) </script>
|