.
This commit is contained in:
parent
5c67a1bcef
commit
56f17f651d
38
uni_modules/zy-mqtt/changelog.md
Normal file
38
uni_modules/zy-mqtt/changelog.md
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
## 1.1.8(2024-08-31)
|
||||||
|
添加获取连接状态函数isConnected,并修改bug
|
||||||
|
## 1.1.7(2024-08-27)
|
||||||
|
将连接失败的报错打印到控制台,方便调试
|
||||||
|
## 1.1.6(2024-07-24)
|
||||||
|
修改解析二进制的jar包
|
||||||
|
## 1.1.5(2024-07-23)
|
||||||
|
返回二进制以及修改文档
|
||||||
|
## 1.1.4(2024-07-23)
|
||||||
|
兼容/主题
|
||||||
|
## 1.1.3(2024-07-23)
|
||||||
|
修改bug
|
||||||
|
## 1.1.2(2024-07-23)
|
||||||
|
兼容返回参数是二进制
|
||||||
|
## 1.1.1(2024-07-23)
|
||||||
|
更新代码
|
||||||
|
## 1.1.0(2024-07-23)
|
||||||
|
兼容订阅消息返回的不同的数据类型
|
||||||
|
## 1.0.9(2024-06-15)
|
||||||
|
代码优化
|
||||||
|
## 1.0.8(2024-06-06)
|
||||||
|
添加息屏保活选项,保证mqtt在后台可持续运行
|
||||||
|
## 1.0.7(2024-06-06)
|
||||||
|
删除UTSJSONobject数据类型
|
||||||
|
## 1.0.6(2024-03-30)
|
||||||
|
新增取消订阅、监听连接丢失、自动重新连接等功能
|
||||||
|
## 1.0.5(2024-03-30)
|
||||||
|
删除class的export
|
||||||
|
## 1.0.4(2024-03-30)
|
||||||
|
删除android下的uts导出default
|
||||||
|
## 1.0.3(2024-03-30)
|
||||||
|
更新config.json
|
||||||
|
## 1.0.2(2024-03-29)
|
||||||
|
更新interface.uts
|
||||||
|
## 1.0.1(2024-03-29)
|
||||||
|
添加interface.uts
|
||||||
|
## 1.0.0(2024-03-29)
|
||||||
|
android端支持mqtt以及rabbitMQ的UTS插件第一次发布
|
BIN
uni_modules/zy-mqtt/encrypt
Normal file
BIN
uni_modules/zy-mqtt/encrypt
Normal file
Binary file not shown.
84
uni_modules/zy-mqtt/package.json
Normal file
84
uni_modules/zy-mqtt/package.json
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
{
|
||||||
|
"id": "zy-mqtt",
|
||||||
|
"displayName": "【新婚福利】android端mqtt插件",
|
||||||
|
"version": "1.1.8",
|
||||||
|
"description": "【新婚福利,这辈子就这几天】android端通过TCP连接mqtt的UTS插件,也可以通过mqtt协议连接rabbitMQ,使用的官方连接mqtt的jar包",
|
||||||
|
"keywords": [
|
||||||
|
"mqtt",
|
||||||
|
"rabbitMQ",
|
||||||
|
"TCP"
|
||||||
|
],
|
||||||
|
"repository": "",
|
||||||
|
"engines": {
|
||||||
|
"HBuilderX": "^4.21"
|
||||||
|
},
|
||||||
|
"dcloudext": {
|
||||||
|
"type": "uts",
|
||||||
|
"sale": {
|
||||||
|
"regular": {
|
||||||
|
"price": "299.00"
|
||||||
|
},
|
||||||
|
"sourcecode": {
|
||||||
|
"price": "999.00"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"contact": {
|
||||||
|
"qq": "1003975792"
|
||||||
|
},
|
||||||
|
"declaration": {
|
||||||
|
"ads": "无",
|
||||||
|
"data": "无",
|
||||||
|
"permissions": "网络"
|
||||||
|
},
|
||||||
|
"npmurl": ""
|
||||||
|
},
|
||||||
|
"uni_modules": {
|
||||||
|
"dependencies": [],
|
||||||
|
"encrypt": [],
|
||||||
|
"platforms": {
|
||||||
|
"cloud": {
|
||||||
|
"tcb": "y",
|
||||||
|
"aliyun": "y",
|
||||||
|
"alipay": "n"
|
||||||
|
},
|
||||||
|
"client": {
|
||||||
|
"Vue": {
|
||||||
|
"vue2": "y",
|
||||||
|
"vue3": "y"
|
||||||
|
},
|
||||||
|
"App": {
|
||||||
|
"app-android": "y",
|
||||||
|
"app-ios": "n"
|
||||||
|
},
|
||||||
|
"H5-mobile": {
|
||||||
|
"Safari": "n",
|
||||||
|
"Android Browser": "n",
|
||||||
|
"微信浏览器(Android)": "n",
|
||||||
|
"QQ浏览器(Android)": "n"
|
||||||
|
},
|
||||||
|
"H5-pc": {
|
||||||
|
"Chrome": "n",
|
||||||
|
"IE": "n",
|
||||||
|
"Edge": "n",
|
||||||
|
"Firefox": "n",
|
||||||
|
"Safari": "n"
|
||||||
|
},
|
||||||
|
"小程序": {
|
||||||
|
"微信": "n",
|
||||||
|
"阿里": "n",
|
||||||
|
"百度": "n",
|
||||||
|
"字节跳动": "n",
|
||||||
|
"QQ": "n",
|
||||||
|
"钉钉": "n",
|
||||||
|
"快手": "n",
|
||||||
|
"飞书": "n",
|
||||||
|
"京东": "n"
|
||||||
|
},
|
||||||
|
"快应用": {
|
||||||
|
"华为": "n",
|
||||||
|
"联盟": "n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
uni_modules/zy-mqtt/readme.md
Normal file
6
uni_modules/zy-mqtt/readme.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# zy-mqtt
|
||||||
|
### 开发文档
|
||||||
|
[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html)
|
||||||
|
[UTS API插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
|
||||||
|
[UTS 组件插件](https://uniapp.dcloud.net.cn/plugin/uts-component.html)
|
||||||
|
[Hello UTS](https://gitcode.net/dcloud/hello-uts)
|
52
uni_modules/zy-mqtt/utssdk/app-android/AndroidManifest.xml
Normal file
52
uni_modules/zy-mqtt/utssdk/app-android/AndroidManifest.xml
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
package="io.dcloud.uni_modules.zy_mqtt">
|
||||||
|
<application>
|
||||||
|
<service android:name="uts.sdk.modules.zyMqtt.ConnectedDeviceService" android:stopWithTask="false"
|
||||||
|
android:foregroundServiceType="connectedDevice" />
|
||||||
|
|
||||||
|
<service android:name="org.eclipse.paho.android.service.MqttService" />
|
||||||
|
</application>
|
||||||
|
|
||||||
|
<!--permissions-->
|
||||||
|
<!-- 通过GPS得到精确位置 -->
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
|
<!-- 通过网络得到粗略位置 -->
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||||
|
<!-- 访问网络,某些位置信息需要从网络服务器获取 -->
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<!-- 访问WiFi状态,需要WiFi信息用于网络定位 -->
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||||
|
<!-- 修改WiFi状态,发起WiFi扫描, 需要WiFi信息用于网络定位 -->
|
||||||
|
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
|
||||||
|
<!-- 访问网络状态, 检测网络的可用性,需要网络运营商相关信息用于网络定位 -->
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
<!-- 访问网络的变化, 需要某些信息用于网络定位 -->
|
||||||
|
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
|
||||||
|
<!-- 蓝牙扫描权限 -->
|
||||||
|
<uses-permission android:name="android.permission.BLUETOOTH" />
|
||||||
|
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
|
||||||
|
<!-- 前台service权限 -->
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
|
<!-- 后台定位权限 -->
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
||||||
|
<!-- A-GPS辅助定位权限,方便GPS快速准确定位 -->
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
|
||||||
|
<!--允许写入扩展存储,用于写入缓存定位数据-->
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<!--用与导航状态中保持屏幕常亮-->
|
||||||
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
|
<!--允许写设备缓存,用于问题排查-->
|
||||||
|
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
|
||||||
|
<!--允许读设备等信息,用于问题排查-->
|
||||||
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||||
|
<!-- 前台service权限 -->
|
||||||
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
|
||||||
|
|
||||||
|
<!-- 电池优化权限 -->
|
||||||
|
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</manifest>
|
14
uni_modules/zy-mqtt/utssdk/app-android/config.json
Normal file
14
uni_modules/zy-mqtt/utssdk/app-android/config.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"abis": [
|
||||||
|
"arm64-v8a", "armeabi-v7a", "armeabi", "x86_64"
|
||||||
|
],
|
||||||
|
"minSdkVersion": 21,
|
||||||
|
"android": {
|
||||||
|
"enableJetifier": true
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"implementation": ["org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5",
|
||||||
|
"org.eclipse.paho:org.eclipse.paho.android.service:1.1.1"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
BIN
uni_modules/zy-mqtt/utssdk/app-android/index.uts
Normal file
BIN
uni_modules/zy-mqtt/utssdk/app-android/index.uts
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
uni_modules/zy-mqtt/utssdk/app-android/service.uts
Normal file
BIN
uni_modules/zy-mqtt/utssdk/app-android/service.uts
Normal file
Binary file not shown.
1
uni_modules/zy-mqtt/utssdk/app-ios/index.uts
Normal file
1
uni_modules/zy-mqtt/utssdk/app-ios/index.uts
Normal file
@ -0,0 +1 @@
|
|||||||
|
<EFBFBD><EFBFBD><EFBFBD>B<EFBFBD><EFBFBD>!<21>p<EFBFBD><16><16>Ђ
|
29
uni_modules/zy-mqtt/utssdk/interface.uts
Normal file
29
uni_modules/zy-mqtt/utssdk/interface.uts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
export type MqttManagerOption = {
|
||||||
|
host : string,
|
||||||
|
clientId : string,
|
||||||
|
userName : string,
|
||||||
|
password : string,
|
||||||
|
cleanSession ?: boolean, // false 创建可持续会话,前提是clientId一直保持不变,
|
||||||
|
automaticReconnect ?: boolean,
|
||||||
|
heartBeat ?: Int,
|
||||||
|
timeOut ?: Int
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type callbackType = (res : UTSJSONObject) => void
|
||||||
|
|
||||||
|
export function connect(option : MqttManagerOption, callback : callbackType) : void
|
||||||
|
|
||||||
|
export function subscribe(topic : string, qos : Int, callback : callbackType) : void
|
||||||
|
|
||||||
|
export function unSubscribe(topic : string, callback : callbackType) : void
|
||||||
|
|
||||||
|
export function publishMessage(topic : string, qos : Int, message : string, callback : callbackType) : void
|
||||||
|
|
||||||
|
export function disConnect(callback : callbackType) : void
|
||||||
|
|
||||||
|
export function onConnectLost(callback : callbackType) : void
|
||||||
|
|
||||||
|
export function onReconnect(callback : callbackType) : void
|
||||||
|
|
||||||
|
export function getConfig() : UTSJSONObject
|
@ -5,6 +5,7 @@ import mqtt from "mqtt/dist/mqtt"
|
|||||||
// #ifdef APP-PLUS
|
// #ifdef APP-PLUS
|
||||||
import {
|
import {
|
||||||
connect,
|
connect,
|
||||||
|
isConnected,
|
||||||
subscribe,
|
subscribe,
|
||||||
unSubscribe,
|
unSubscribe,
|
||||||
getConfig,
|
getConfig,
|
||||||
@ -24,11 +25,21 @@ class MqttUtil {
|
|||||||
static client;
|
static client;
|
||||||
static connected = false;
|
static connected = false;
|
||||||
static reconnectCount = 0;
|
static reconnectCount = 0;
|
||||||
|
onCallback = (data) => {
|
||||||
|
console.error("onCallback:", data)
|
||||||
|
};
|
||||||
|
|
||||||
//接收创建来的数据
|
//接收创建来的数据
|
||||||
constructor(eventHandler) {
|
constructor(eventHandler) {
|
||||||
this.url = import.meta.env.VITE_BASE_MQTT;
|
|
||||||
this.eventHandler = eventHandler;
|
this.eventHandler = eventHandler;
|
||||||
|
const url = import.meta.env.VITE_BASE_MQTT;
|
||||||
|
|
||||||
|
// #ifdef H5
|
||||||
|
this.url = `mqtt://${url}`;
|
||||||
|
// #endif
|
||||||
|
// #ifdef APP-PLUS
|
||||||
|
this.url = `ws://${url}`;
|
||||||
|
// #endif
|
||||||
}
|
}
|
||||||
//初始化参数
|
//初始化参数
|
||||||
init(userId) {
|
init(userId) {
|
||||||
@ -37,7 +48,6 @@ class MqttUtil {
|
|||||||
let password = "app_client@2023";
|
let password = "app_client@2023";
|
||||||
let keepalive = 10; //保持连接的心跳间隔,单位为秒。
|
let keepalive = 10; //保持连接的心跳间隔,单位为秒。
|
||||||
let connectTimeout = 30; //接超时的时间,单位为秒。
|
let connectTimeout = 30; //接超时的时间,单位为秒。
|
||||||
|
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
this.options = {
|
this.options = {
|
||||||
clientId: clientId, // string 客户端的唯一标识符。
|
clientId: clientId, // string 客户端的唯一标识符。
|
||||||
@ -77,19 +87,9 @@ class MqttUtil {
|
|||||||
}
|
}
|
||||||
//监听消息回调
|
//监听消息回调
|
||||||
onEventHandler(topic, data) {
|
onEventHandler(topic, data) {
|
||||||
// console.error("topic:", topic);
|
console.error("topic:", topic);
|
||||||
// console.error("data:", data);
|
console.error("data:", data);
|
||||||
this.eventHandler(topic, data);
|
this.eventHandler(topic, data);
|
||||||
// var message = JSON.stringify(res);
|
|
||||||
// // console.error("message:",message);
|
|
||||||
// if (x.includes("jsy/iot/control/")) {
|
|
||||||
// // uni.$emit("mqtt-message-refresh", message)
|
|
||||||
// } else {
|
|
||||||
// if (message !== "" && message !== null) {
|
|
||||||
// let str = JSON.parse(message)
|
|
||||||
// uni.$emit("mqtt-message", str)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
getUUID() {
|
getUUID() {
|
||||||
function S4() {
|
function S4() {
|
||||||
@ -104,20 +104,17 @@ class MqttUtil {
|
|||||||
if (!this.options) {
|
if (!this.options) {
|
||||||
this.init(userId);
|
this.init(userId);
|
||||||
}
|
}
|
||||||
console.error("link:", this.client)
|
|
||||||
console.error("开始连接")
|
|
||||||
|
|
||||||
this.client = mqtt.connect(this.url, this.options);
|
this.client = mqtt.connect(this.url, this.options);
|
||||||
|
|
||||||
this.client.on("connect", error => {
|
this.client.on("connect", error => {
|
||||||
this.connected = this.client.connected && !this.client.reconnecting;
|
this.connected = this.client.connected && !this.client.reconnecting;
|
||||||
this.reconnectCount = 0;
|
this.reconnectCount = 0;
|
||||||
console.error("消息连接成功:", this.client)
|
console.log("消息连接成功:", this.client)
|
||||||
})
|
})
|
||||||
this.client.on("reconnect", error => {
|
this.client.on("reconnect", error => {
|
||||||
this.connected = this.client.connected && !this.client.reconnecting;
|
this.connected = this.client.connected && !this.client.reconnecting;
|
||||||
this.reconnectCount++;
|
this.reconnectCount++;
|
||||||
console.error(`消息重连中${this.reconnectCount}`, this.client)
|
console.log(`消息重连中${this.reconnectCount}`, this.client)
|
||||||
})
|
})
|
||||||
this.client.on("error", error => {
|
this.client.on("error", error => {
|
||||||
console.log("error", error)
|
console.log("error", error)
|
||||||
@ -142,7 +139,7 @@ class MqttUtil {
|
|||||||
subscribes(topic) {
|
subscribes(topic) {
|
||||||
this.client.subscribe(topic, error => {
|
this.client.subscribe(topic, error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(`订阅主题失败:`, error)
|
console.log(`订阅主题失败:`, error)
|
||||||
} else {
|
} else {
|
||||||
console.log(`订阅主题成功`)
|
console.log(`订阅主题成功`)
|
||||||
}
|
}
|
||||||
@ -172,35 +169,34 @@ class MqttUtil {
|
|||||||
if (!this.options) {
|
if (!this.options) {
|
||||||
this.init(userId);
|
this.init(userId);
|
||||||
}
|
}
|
||||||
console.error("link:")
|
|
||||||
console.error("开始连接")
|
|
||||||
|
|
||||||
// 先连接mqtt
|
// 先连接mqtt
|
||||||
connect(this.options, (res) => {
|
connect(this.options, (res) => {
|
||||||
console.error("连接成功:", res);
|
console.log("connect:", res);
|
||||||
if (res.code == 200) {
|
if (res.code == 200) {
|
||||||
this.connected = true;
|
this.connected = isConnected();
|
||||||
this.reconnectCount = 0;
|
this.reconnectCount = 0;
|
||||||
console.error("消息连接成功:")
|
console.log("连接成功:", this.connected)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// 监听连接丢失函数
|
// 监听连接丢失函数
|
||||||
onConnectLost((res) => {
|
onConnectLost((res) => {
|
||||||
console.error("连接丢失的原因:", res.message);
|
console.log("连接丢失的原因:", res);
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
})
|
})
|
||||||
// 监听自动重新连接函数
|
// 监听自动重新连接函数
|
||||||
onReconnect((res) => {
|
onReconnect((res) => {
|
||||||
|
console.log("消息重连中:", res);
|
||||||
// this.connected = this.client.connected && !this.client.reconnecting;
|
// this.connected = this.client.connected && !this.client.reconnecting;
|
||||||
this.connected = true;
|
this.connected = isConnected();
|
||||||
this.reconnectCount++;
|
this.reconnectCount++;
|
||||||
console.error(`消息重连中${this.reconnectCount}`)
|
console.log(`消息重连中${this.reconnectCount}:`, this.connected)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
//结束链接
|
//结束链接
|
||||||
over() {
|
over() {
|
||||||
disConnect((res) => {
|
disConnect((res) => {
|
||||||
this.connected = false;
|
console.log("接受消息:", res)
|
||||||
|
this.connected = isConnected();
|
||||||
this.reconnectCount = 0;
|
this.reconnectCount = 0;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -208,19 +204,18 @@ class MqttUtil {
|
|||||||
subscribes(topics) {
|
subscribes(topics) {
|
||||||
if (topics && topics.length) {
|
if (topics && topics.length) {
|
||||||
topics.forEach(x => {
|
topics.forEach(x => {
|
||||||
subscribe(topic, 0, (res) => {
|
subscribe(x, 0, (res) => {
|
||||||
console.log("接受消息")
|
console.log("接受消息")
|
||||||
console.log("message:", message.toString())
|
|
||||||
var message = JSON.stringify(res);
|
var message = JSON.stringify(res);
|
||||||
this.onEventHandler(topic, message);
|
console.log("message:", message.toString());
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//取消订阅
|
//取消订阅
|
||||||
unsubscribes(topic) {
|
unsubscribes(topics) {
|
||||||
if (topic && topic.length) {
|
if (topics && topics.length) {
|
||||||
topic.forEach(x => {
|
topics.forEach(x => {
|
||||||
unSubscribe(x, (res) => {})
|
unSubscribe(x, (res) => {})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user