更新记录

1.0.1(2026-04-30) 下载此版本

已弃坑UTS,为了方便技术研究,遂决定开源,不再积极维护

1.0.0(2026-04-14) 下载此版本

新增功能

  • TCP 客户端支持(基于 Kotlin NIO)
  • UDP 通信支持(单播、广播、组播)
  • 多实例管理
  • 多种数据格式支持(String、Hex、Uint8Array)
  • TCP 分包粘包处理
  • 心跳机制
  • 自动重连(指数退避算法)
  • 完整的事件回调系统
  • 并发控制和线程安全

平台兼容性

uni-app(4.41)

Vue2 Vue3 Vue3插件版本 Chrome Safari app-vue app-nvue Android Android插件版本 iOS 鸿蒙
- 1.0.0 - - - - 5.0 1.0.0 × ×
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - - -

uni-app x(4.41)

Chrome Safari Android Android插件版本 iOS 鸿蒙 微信小程序
- - 5.0 1.0.0 × × -

Changbi Network 插件

功能完善的 TCP/UDP 网络通信 UTS 插件,支持安卓平台。

核心特性

基础连接管理

  • 多实例支持:允许同时创建多个 TCP 或 UDP 实例
  • 唯一 ID 区分:每个实例通过唯一 ID 管理

TCP 客户端功能

  • connect(host, port, timeout) - 异步连接,支持超时配置
  • disconnect() - 安全断开连接
  • 自动重连机制(可配置)

UDP 通信功能

  • bind(port) - 绑定本地端口
  • send(data, host, port) - 单播发送
  • setBroadcast(boolean) - 开启/关闭广播
  • setMulticast(groupAddr) - 加入组播组

数据格式支持

  • String(支持 UTF-8, GBK 等编码)
  • Hex(十六进制字符串)
  • Uint8Array / ArrayBuffer(零拷贝)

TCP 分包与粘包处理

  • 定界符解析(\n, \r\n 等)
  • 固定包头长度解析

心跳机制

  • 自动发送心跳包
  • 维持长连接活跃

事件回调

  • onConnect - TCP 连接成功
  • onMessage - 收到数据
  • onClose - 连接关闭
  • onError - 发生错误
  • onSend - 数据发送确认

健壮性特性

  • 自动重连(指数退避算法)
  • 并发控制
  • 线程安全

快速开始

import * as network from '@/uni_modules/changbi-network'

// 创建 TCP 客户端
const tcpId = network.createTcpClient({
  onConnect: () => {
    console.log('TCP 连接成功')
  },
  onMessage: (data) => {
    console.log('收到数据:', data)
  },
  onClose: () => {
    console.log('连接关闭')
  },
  onError: (error) => {
    console.error('错误:', error)
  }
})

// 连接服务器
await network.connect(tcpId, {
  host: '192.168.1.100',
  port: 8080,
  timeout: 5000,
  autoReconnect: true,
  reconnectInterval: 3000,
  maxReconnectAttempts: 5
})

// 发送数据
await network.send(tcpId, 'Hello Server', 'string')

// 断开连接
await network.disconnect(tcpId)

详细文档

技术实现

  • 使用 Kotlin NIO (SocketChannel) 实现 TCP
  • 使用 DatagramChannel 实现 UDP
  • 协程处理异步操作
  • 线程安全的实例管理

许可证

MIT License

隐私、权限声明

1. 本插件需要申请的系统权限列表:

android.permission.INTERNET,android.permission.ACCESS_NETWORK_STATE

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT License

Copyright (c) 2026 Changbi Network Plugin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.