From b804d770eaa53656a9591ece4cb3803675ba3044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AE=87?= <478110507@qq.com> Date: Thu, 4 Jul 2024 12:41:51 +0000 Subject: [PATCH] =?UTF-8?q?docs(readme):=20=E6=96=B0=E5=A2=9Ereact-native-?= =?UTF-8?q?mqtt=E6=8C=87=E5=AF=BC=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张宇 <478110507@qq.com> --- zh-cn/react-native-mqtt.md | 183 +++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 zh-cn/react-native-mqtt.md diff --git a/zh-cn/react-native-mqtt.md b/zh-cn/react-native-mqtt.md new file mode 100644 index 00000000..ec40bebc --- /dev/null +++ b/zh-cn/react-native-mqtt.md @@ -0,0 +1,183 @@ + + +模板版本:v0.2.2 + +

+

+

+

+ + Supported platforms + + + License + + +

+ + + + + +> [!TIP] [Github 地址](https://github.com/Introvertuous/react-native-mqtt) + +## 安装与使用 + +进入到工程目录并输入以下命令: + + + +#### **npm** + +```bash +npm i react_native_mqtt@1.3.1 +``` + +#### **yarn** + +```bash +yarn add react_native_mqtt@1.3.1 +``` + + + +基本使用场景: + +> [!WARNING] 使用时 import 的库名不变。(本组件基于MQTT 3.1.1协议) + +```js +import init from 'react_native_mqtt'; +import { AsyncStorage } from 'react-native'; + +//初始化参数 +init({ + size: 10000, + storageBackend: AsyncStorage, + defaultExpires: 1000 * 3600 * 24, + enableCache: true, + reconnect: true, + sync: {} +}); +//MQTT服务器配置 +const options = { + host: 'i1b2c58a.ala.dedicated.aliyun.emqxcloud.cn', + port: 8083, + path: '/mqtt', + userName: 'test1', + password: '123456', + keepAliveInterval: 600, + cleanSession: true, + clientId: 'mqttTestDemo' + Math.random().toString(16).substring(2, 8) +}; +//连接成功 +function onConnect() { + client.subscribe('testTopic', { qos: 0 }); + console.info("Connect Success! "); +} +//连接失败 +function onFailure(err) { + if (err.errorCode !== 0) { + console.error('Connect Failed! ' + err.errorMessage); + } +} +//连接丢失 +function onConnectionLost(err) { + if (err.errorCode !== 0) { + console.error("onConnectionLost:" + err.errorMessage); + } +} +//接收消息 +function onMessageArrived(message) { + console.info('onMessageArrived: ' + message.payloadString); +} +//连接服务器 +const client = new Paho.MQTT.Client(options.host, options.port, options.path, options.clientId); +client.onConnectionLost = onConnectionLost; +client.onMessageArrived = onMessageArrived; +client.connect({ + onSuccess: onConnect, + onFailure: onFailure, + useSSL: false, + userName: options.userName, + password: options.password, + keepAliveInterval: options.keepAliveInterval, + cleanSession: options.cleanSession +}); +//取消连接 +function disconnect() { + client.disconnect(); + console.info("DisConnect Success! "); +} +//订阅主题 +function subscribe() { + client.subscribe(onChangeTopic, { qos: 0 }); + console.info("subscribe success! "); +} +//取消订阅 +function unSubscribe() { + client.unsubscribe(onChangeTopic); + console.info("unsubscribe success! "); +} +//消息发布 +function sendMessage() { + var message = new Paho.MQTT.Message(onChangeMessage); + message.destinationName = topic; + client.send(message); + console.info('send success! ') +} +``` + +## 约束与限制 + +### 兼容性 + +本文档内容基于以下版本验证通过: + +1. RNOH: 0.72.26; SDK:HarmonyOS NEXT Developer Beta1; IDE:DevEco Studio 5.0.3.300; ROM:3.0.0.25; + +## 属性 + +> [!tip] "Platform"列表示该属性在原三方库上支持的平台。 + +> [!tip] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。 + +| Name | Description | Type | Required | Platform | HarmonyOS Support | +| ----------------- | ---------------------------------------------------- | ------ | -------- | ------------ | ----------------- | +| host | 服务器地址 | string | yes | Android、IOS | yes | +| port | 服务器端口 | number | yes | Android、IOS | yes | +| path | 服务器路径 | string | yes | Android、IOS | yes | +| userName | 用户名 | string | yes | Android、IOS | yes | +| password | 密码 | string | yes | Android、IOS | yes | +| keepAliveInterval | 会话过期时间(秒) | number | no | Android、IOS | yes | +| cleanSession | 消息持久化(值为TRUE时,当客户端掉线,清除以前消息) | bool | no | Android、IOS | yes | +| clientId | 客户端ID | string | yes | Android、IOS | yes | +| useSSL | SSL/TLS加密通信 | bool | yes | Android、IOS | yes | +| qos | 服务级别质量 | number | yes | Android、IOS | yes | + +## 静态方法 + +> [!tip] "Platform"列表示该属性在原三方库上支持的平台。 + +> [!tip] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。 + +| Name | Description | Type | Required | Platform | HarmonyOS Support | +| ---------------- | ------------------ | ---- | -------- | ------------ | ----------------- | +| client.connect | 连接MQTT服务器 | func | yes | Android、IOS | yes | +| onConnect | 连接成功时的回调 | func | no | Android、IOS | yes | +| onFailure | 连接失败时的回调 | func | no | Android、IOS | yes | +| onConnectionLost | 连接丢失 | func | no | Android、IOS | yes | +| onMessageArrived | 接收消息 | func | no | Android、IOS | yes | +| disconnect | 取消连接MQTT服务器 | func | no | Android、IOS | yes | +| subscribe | 订阅主题topic | func | no | Android、IOS | yes | +| unSubscribe | 取消订阅topic | func | no | Android、IOS | yes | +| sendMessage | 发布消息 | func | no | Android、IOS | yes | + +## 遗留问题 + +## 其他 + +## 开源协议 + +本项目基于 [The MIT License (MIT)](https://github.com/Introvertuous/react-native-mqtt?tab=MIT-1-ov-file) ,请自由地享受和参与开源。 + + \ No newline at end of file -- Gitee