diff --git a/uni_modules/zy-mqtt/changelog.md b/uni_modules/zy-mqtt/changelog.md new file mode 100644 index 0000000..4b5510c --- /dev/null +++ b/uni_modules/zy-mqtt/changelog.md @@ -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插件第一次发布 diff --git a/uni_modules/zy-mqtt/encrypt b/uni_modules/zy-mqtt/encrypt new file mode 100644 index 0000000..ac5402b Binary files /dev/null and b/uni_modules/zy-mqtt/encrypt differ diff --git a/uni_modules/zy-mqtt/package.json b/uni_modules/zy-mqtt/package.json new file mode 100644 index 0000000..76390a8 --- /dev/null +++ b/uni_modules/zy-mqtt/package.json @@ -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" + } + } + } + } +} \ No newline at end of file diff --git a/uni_modules/zy-mqtt/readme.md b/uni_modules/zy-mqtt/readme.md new file mode 100644 index 0000000..e8a694a --- /dev/null +++ b/uni_modules/zy-mqtt/readme.md @@ -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) \ No newline at end of file diff --git a/uni_modules/zy-mqtt/utssdk/app-android/AndroidManifest.xml b/uni_modules/zy-mqtt/utssdk/app-android/AndroidManifest.xml new file mode 100644 index 0000000..7b3be2b --- /dev/null +++ b/uni_modules/zy-mqtt/utssdk/app-android/AndroidManifest.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/uni_modules/zy-mqtt/utssdk/app-android/config.json b/uni_modules/zy-mqtt/utssdk/app-android/config.json new file mode 100644 index 0000000..ca86c1c --- /dev/null +++ b/uni_modules/zy-mqtt/utssdk/app-android/config.json @@ -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" + ] + } +} \ No newline at end of file diff --git a/uni_modules/zy-mqtt/utssdk/app-android/index.uts b/uni_modules/zy-mqtt/utssdk/app-android/index.uts new file mode 100644 index 0000000..af493d4 Binary files /dev/null and b/uni_modules/zy-mqtt/utssdk/app-android/index.uts differ diff --git a/uni_modules/zy-mqtt/utssdk/app-android/libs/org.eclipse.paho.android.service-1.1.1.jar b/uni_modules/zy-mqtt/utssdk/app-android/libs/org.eclipse.paho.android.service-1.1.1.jar new file mode 100644 index 0000000..c4a1090 Binary files /dev/null and b/uni_modules/zy-mqtt/utssdk/app-android/libs/org.eclipse.paho.android.service-1.1.1.jar differ diff --git a/uni_modules/zy-mqtt/utssdk/app-android/libs/org.eclipse.paho.client.mqttv3-1.2.5.jar b/uni_modules/zy-mqtt/utssdk/app-android/libs/org.eclipse.paho.client.mqttv3-1.2.5.jar new file mode 100644 index 0000000..66f1278 Binary files /dev/null and b/uni_modules/zy-mqtt/utssdk/app-android/libs/org.eclipse.paho.client.mqttv3-1.2.5.jar differ diff --git a/uni_modules/zy-mqtt/utssdk/app-android/service.uts b/uni_modules/zy-mqtt/utssdk/app-android/service.uts new file mode 100644 index 0000000..061f652 Binary files /dev/null and b/uni_modules/zy-mqtt/utssdk/app-android/service.uts differ diff --git a/uni_modules/zy-mqtt/utssdk/app-ios/index.uts b/uni_modules/zy-mqtt/utssdk/app-ios/index.uts new file mode 100644 index 0000000..51dc5ca --- /dev/null +++ b/uni_modules/zy-mqtt/utssdk/app-ios/index.uts @@ -0,0 +1 @@ +B!pЂ \ No newline at end of file diff --git a/uni_modules/zy-mqtt/utssdk/interface.uts b/uni_modules/zy-mqtt/utssdk/interface.uts new file mode 100644 index 0000000..ac5b0c2 --- /dev/null +++ b/uni_modules/zy-mqtt/utssdk/interface.uts @@ -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 \ No newline at end of file diff --git a/utils/mqttUtil.js b/utils/mqttUtil.js index 15d61e9..2c33c41 100644 --- a/utils/mqttUtil.js +++ b/utils/mqttUtil.js @@ -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) => {}) }); }