This commit is contained in:
Iruka 2024-09-30 10:11:05 +08:00
parent 5c67a1bcef
commit 56f17f651d
13 changed files with 256 additions and 37 deletions

View File

@ -0,0 +1,38 @@
## 1.1.82024-08-31
添加获取连接状态函数isConnected并修改bug
## 1.1.72024-08-27
将连接失败的报错打印到控制台,方便调试
## 1.1.62024-07-24
修改解析二进制的jar包
## 1.1.52024-07-23
返回二进制以及修改文档
## 1.1.42024-07-23
兼容/主题
## 1.1.32024-07-23
修改bug
## 1.1.22024-07-23
兼容返回参数是二进制
## 1.1.12024-07-23
更新代码
## 1.1.02024-07-23
兼容订阅消息返回的不同的数据类型
## 1.0.92024-06-15
代码优化
## 1.0.82024-06-06
添加息屏保活选项保证mqtt在后台可持续运行
## 1.0.72024-06-06
删除UTSJSONobject数据类型
## 1.0.62024-03-30
新增取消订阅、监听连接丢失、自动重新连接等功能
## 1.0.52024-03-30
删除class的export
## 1.0.42024-03-30
删除android下的uts导出default
## 1.0.32024-03-30
更新config.json
## 1.0.22024-03-29
更新interface.uts
## 1.0.12024-03-29
添加interface.uts
## 1.0.02024-03-29
android端支持mqtt以及rabbitMQ的UTS插件第一次发布

BIN
uni_modules/zy-mqtt/encrypt Normal file

Binary file not shown.

View 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"
}
}
}
}
}

View 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)

View 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>

View 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"
]
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
<EFBFBD><EFBFBD><EFBFBD>B<EFBFBD><EFBFBD>!<21>p<EFBFBD><16><16>Ђ

View 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

View File

@ -5,6 +5,7 @@ import mqtt from "mqtt/dist/mqtt"
// #ifdef APP-PLUS
import {
connect,
isConnected,
subscribe,
unSubscribe,
getConfig,
@ -24,11 +25,21 @@ class MqttUtil {
static client;
static connected = false;
static reconnectCount = 0;
onCallback = (data) => {
console.error("onCallback:", data)
};
//接收创建来的数据
constructor(eventHandler) {
this.url = import.meta.env.VITE_BASE_MQTT;
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) {
@ -36,8 +47,7 @@ class MqttUtil {
let username = "app_client";
let password = "app_client@2023";
let keepalive = 10; //保持连接的心跳间隔,单位为秒。
let connectTimeout = 30; //接超时的时间,单位为秒。
let connectTimeout = 30; //接超时的时间,单位为秒。
// #ifdef H5
this.options = {
clientId: clientId, // string 客户端的唯一标识符。
@ -77,19 +87,9 @@ class MqttUtil {
}
//监听消息回调
onEventHandler(topic, data) {
// console.error("topic", topic);
// console.error("data", data);
console.error("topic", topic);
console.error("data", 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() {
function S4() {
@ -104,20 +104,17 @@ class MqttUtil {
if (!this.options) {
this.init(userId);
}
console.error("link:", this.client)
console.error("开始连接")
this.client = mqtt.connect(this.url, this.options);
this.client.on("connect", error => {
this.connected = this.client.connected && !this.client.reconnecting;
this.reconnectCount = 0;
console.error("消息连接成功:", this.client)
console.log("消息连接成功:", this.client)
})
this.client.on("reconnect", error => {
this.connected = this.client.connected && !this.client.reconnecting;
this.reconnectCount++;
console.error(`消息重连中${this.reconnectCount}`, this.client)
console.log(`消息重连中${this.reconnectCount}`, this.client)
})
this.client.on("error", error => {
console.log("error", error)
@ -142,7 +139,7 @@ class MqttUtil {
subscribes(topic) {
this.client.subscribe(topic, error => {
if (error) {
console.error(`订阅主题失败:`, error)
console.log(`订阅主题失败:`, error)
} else {
console.log(`订阅主题成功`)
}
@ -172,35 +169,34 @@ class MqttUtil {
if (!this.options) {
this.init(userId);
}
console.error("link:")
console.error("开始连接")
// 先连接mqtt
connect(this.options, (res) => {
console.error("连接成功", res);
console.log("connect", res);
if (res.code == 200) {
this.connected = true;
this.connected = isConnected();
this.reconnectCount = 0;
console.error("消息连接成功:")
console.log("连接成功:", this.connected)
}
})
// 监听连接丢失函数
onConnectLost((res) => {
console.error("连接丢失的原因:", res.message);
console.log("连接丢失的原因:", res);
this.connected = false;
})
// 监听自动重新连接函数
onReconnect((res) => {
console.log("消息重连中:", res);
// this.connected = this.client.connected && !this.client.reconnecting;
this.connected = true;
this.connected = isConnected();
this.reconnectCount++;
console.error(`消息重连中${this.reconnectCount}`)
console.log(`消息重连中${this.reconnectCount}:`, this.connected)
})
}
//结束链接
over() {
disConnect((res) => {
this.connected = false;
console.log("接受消息:", res)
this.connected = isConnected();
this.reconnectCount = 0;
})
}
@ -208,19 +204,18 @@ class MqttUtil {
subscribes(topics) {
if (topics && topics.length) {
topics.forEach(x => {
subscribe(topic, 0, (res) => {
subscribe(x, 0, (res) => {
console.log("接受消息")
console.log("message", message.toString())
var message = JSON.stringify(res);
this.onEventHandler(topic, message);
console.log("message", message.toString());
})
});
}
}
//取消订阅
unsubscribes(topic) {
if (topic && topic.length) {
topic.forEach(x => {
unsubscribes(topics) {
if (topics && topics.length) {
topics.forEach(x => {
unSubscribe(x, (res) => {})
});
}