使用 WebSockets 连接 MQTT 服务器实现家庭自动化 LED 控制
1. 使用 WebSockets 连接 MQTT 服务器
在 JavaScript 中,我们可以使用 Paho.MQTT.Client 来创建一个 MQTT over WebSockets 客户端,实现与 MQTT 服务器的连接。以下是相关代码:
APP.HomeAutomation.Manager = {
onConnectSuccess: function(invocationContext) {
// Update the status text
document.getElementById("status").textContent = "Connected with the MQTT Server";
// Now, subscribe to home/results/leds/1, home/results/leds/2 and
// home/results/leds/3
var client = invocationContext.invocationContext.client;
for (var i = 1; i < 4; i++) {
client.subscribe("home/results/leds/" + i);
}
},
connect: function() {
this.client = new Paho.
超级会员免费看
订阅专栏 解锁全文
5万+

被折叠的 条评论
为什么被折叠?



