第17章 Mosquitto WebSocket支持

第17章 WebSocket支持

17.1 WebSocket over MQTT

WebSocket
TCP/MQTT
浏览器
Mosquitto
原生客户端
协议转换
WebSocket帧
MQTT报文

17.2 配置WebSocket

bash 复制代码
# /etc/mosquitto/mosquitto.conf

# MQTT监听器
listener 1883
protocol mqtt

# WebSocket监听器
listener 9001
protocol websockets
allow_anonymous true

# 或者需要认证
listener 9001
protocol websockets
allow_anonymous false
password_file /etc/mosquitto/passwd

17.3 Web客户端实现

HTML示例

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>MQTT Web Client</title>
    <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
</head>
<body>
    <h1>MQTT Dashboard</h1>
    <div>
        <input type="text" id="topic" placeholder="Topic">
        <input type="text" id="message" placeholder="Message">
        <button onclick="publish()">Publish</button>
    </div>
    <div id="output"></div>

    <script>
        const client = mqtt.connect('ws://localhost:9001');

        client.on('connect', () => {
            console.log('Connected');
            client.subscribe('sensor/#');
        });

        client.on('message', (topic, message) => {
            const output = document.getElementById('output');
            output.innerHTML += `<p>${topic}: ${message}</p>`;
        });

        function publish() {
            const topic = document.getElementById('topic').value;
            const message = document.getElementById('message').value;
            client.publish(topic, message);
        }
    </script>
</body>
</html>

Vue.js示例

vue 复制代码
<template>
  <div>
    <h1>MQTT Monitor</h1>
    <div v-for="(msg, index) in messages" :key="index">
      {{ msg.topic }}: {{ msg.payload }}
    </div>
  </div>
</template>

<script>
import mqtt from 'mqtt';

export default {
  data() {
    return {
      client: null,
      messages: []
    };
  },
  mounted() {
    this.client = mqtt.connect('ws://localhost:9001');

    this.client.on('connect', () => {
      this.client.subscribe('sensor/#');
    });

    this.client.on('message', (topic, message) => {
      this.messages.push({
        topic,
        payload: message.toString()
      });
    });
  },
  beforeUnmount() {
    this.client.end();
  }
};
</script>

17.4 跨域配置

bash 复制代码
# 需要反向代理处理跨域

# Nginx配置
location /mqtt {
    proxy_pass http://localhost:9001;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

17.5 实时应用案例

传感器
Mosquitto
WebSocket客户端
原生客户端
Web仪表板
实时图表
地图监控

17.6 本章小结

掌握了Mosquitto的WebSocket配置和Web客户端开发。

相关推荐
Yang9611几秒前
鼎讯信通热剥钳实测:大芯径光纤如何做到无损剥除
网络
贞明电子8 小时前
工业三色报警灯怎么接入项目系统?开关量与RS485控制应用指南
运维·网络·微信公众平台·贞明电子
明天也要努力学习9 小时前
HTTP 协议的基本格式和 fiddler 的用法
网络协议·http·fiddler
HideF9 小时前
Linux
linux·服务器·网络
万联WANFLOW9 小时前
海外网络故障怎么排查?Cloudflare 三季度断网报告解读与 mtr 实战
运维·网络·故障分析
sbjdhjd9 小时前
网工进阶笔记:H3CSE 园区网全栈技术与协议汇总
网络·笔记·云计算·ssh·网络工程·vlan·aaa
taktok_ai11 小时前
用 cc-switch 连接 Taktok
网络
小嘉丶学长13 小时前
Day14 · 字典与集合
java·开发语言·网络·python·学习·1024程序员节
pt104315 小时前
Cisco设备网络安全系列-18:SIEM与SOAR安全解决方案
网络·安全
Tel1992530800415 小时前
温度湿度信号转WiFi/MQTT/Modbus TCP采集模块 无线传输
网络·网络协议·tcp/ip·工业自动化·工控设备