# MQTT应用指南

### MQTT 发布订阅

&#x20;MQTT 是为移动互联网、物联网设计的轻量发布订阅模式的消息服务器:

![](/files/-LQXxTrU0pb8Uy4JFybz)

&#x20;CP7消息服务器安装启动后，任何设备或终端的 MQTT 客户端，可通过 MQTT 协议连接到服务器，发布订阅消息方式互通。

&#x20;MQTT 协议客户端库: <https://github.com/mqtt/mqtt.github.io/wiki/libraries>

&#x20;例如，mosquitto\_sub/pub 命令行发布订阅消息:

```
mosquitto_sub -t topic -q 2
mosquitto_pub -t topic -q 1 -m "Hello, MQTT!"
```

### MQTT WebSocket 连接

CP7 消息服务器支持 MQTT WebSocket 连接，Web 浏览器可直接通过 MQTT 协议连接服务器:

| WebSocket URI:          | ws(s)://host:8083/         |
| ----------------------- | -------------------------- |
| Sec-WebSocket-Protocol: | ‘mqttv3.1’ or ‘mqttv3.1.1’ |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coolpy7.gitbook.io/coolpy7book/mqtt-xie-yi-ji-chu-zhi-shi/yong-hu-zhi-nan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
