「MQTT訂閱與發行」:修訂間差異
跳至導覽
跳至搜尋
已建立頁面,內容為 "==javascript== <html> <!DOCTYPE html> <html> <head> <title>MQTT訂閱範例</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/mqtt/4.1.0/mqtt.min.js">…" |
|||
| 第1行: | 第1行: | ||
==javascript== | ==javascript== | ||
< | <php> | ||
<!DOCTYPE html> | <!DOCTYPE html> | ||
| 第34行: | 第34行: | ||
</body> | </body> | ||
</html> | </html> | ||
</php> | |||
</ | |||
於 2023年3月24日 (五) 06:59 的修訂
javascript
<php>
<!DOCTYPE html> <html> <head>
<title>MQTT訂閱範例</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/mqtt/4.1.0/mqtt.min.js"></script> <script> const client = mqtt.connect('ws://192.168.1.57:1883'); client.on('connect', function () { client.subscribe('kb/kb99', function (err) { if (!err) { console.log('訂閱成功') } }) }) client.on('message', function (topic, message) { console.log('接收到消息:', message.toString()) })
/*
client.on('connect', function () {
client.publish('kb/kb99','test');
})
- /
</script>
</head> <body>
MQTT訂閱範例
開啟瀏覽器的開發者工具並檢查控制台輸出
</body> </html> </php>