快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-mqtt`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/mqtt/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-mqtt` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
npm install @capgo/capacitor-mqttnpx cap sync导入
“导入”章节import { MqttBridge } from '@capgo/capacitor-mqtt';快速入门
章节标题为“快速入门”使用 TLS 进行连接,启用自动重连,然后订阅后端发布的遥测主题。
import { MqttBridge } from '@capgo/capacitor-mqtt';
await MqttBridge.connect({ serverURI: 'ssl://broker.example.com', port: 8883, clientId: 'cap-app-1', username: 'device-user', password: 'device-secret', setCleanSession: true, connectionTimeout: 30, keepAliveInterval: 60, setAutomaticReconnect: true,});await MqttBridge.subscribe({ topic: 'devices/telemetry', qos: 1 });常见坑点
章节标题为“常见陷阱”- TLS: 匹配
serverURI符合 Broker 的协议要求。 - 后台: 在 Android 挂起 WebView 后自动重连。
- QoS:较高的 QoS 会增加蜂窝网络下的电池消耗。
API 概览
章节标题:“API 概览”connect
“connect” 章节查看当前合约的源码定义。
import { MqttBridge } from '@capgo/capacitor-mqtt';
const result = await MqttBridge.connect({ serverURI: 'https://example.com', port: 8080, clientId: 'client-id-123', username: 'username', password: 'password', setCleanSession: true, connectionTimeout: 1000, keepAliveInterval: 1000, setAutomaticReconnect: true,});console.log(result);disconnect
“disconnect” 章节查看当前合约的源码定义。
import { MqttBridge } from '@capgo/capacitor-mqtt';
const result = await MqttBridge.disconnect();console.log(result);subscribe
标题为 “subscribe” 的章节查看当前合约的源码定义。
import { MqttBridge } from '@capgo/capacitor-mqtt';
const result = await MqttBridge.subscribe({ topic: 'topic', qos: 1,});console.log(result);publish
“publish” 章节查看当前合约的源码定义。
import { MqttBridge } from '@capgo/capacitor-mqtt';
const result = await MqttBridge.publish({ topic: 'topic', payload: 'payload', qos: 1, retained: true,});console.log(result);类型参考
“类型参考” 章节onConnectionLostListener
“onConnectionLostListener” 章节export type onConnectionLostListener = (x: { connectionStatus: string; reasonCode: number; message: string }) => void;onConnectCompleteListener
“onConnectCompleteListener” 章节export type onConnectCompleteListener = (x: { reconnected: boolean; serverURI: string }) => void;onMessageArrivedListener
“onMessageArrivedListener” 章节export type onMessageArrivedListener = (x: { topic: string; message: string }) => void;单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/capacitor-mqtt 在“使用 @capgo/capacitor-mqtt”中实现原生能力, API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。