
sl-uniplugin-tcp-udp TCP/UDP 网络通信插件插件市场操作视频简介基于 JavaSocket/DatagramSocket的TCP/UDP 网络通信UTS 插件支持TCP 客户端连接、发送、接收、关闭TCP 服务端监听端口、接收客户端数据、向客户端发送数据UDP 发送与接收所有收发数据均使用 UTF-8 编解码支持中文传输。目录结构sl-uniplugin-tcp-udp/ ├── package.json ├── README.md └── utssdk/app-android/ ├── index.uts # TCP/UDP 实现 ├── config.json # 依赖与构建配置 └── AndroidManifest.xml # 权限声明 (INTERNET, ACCESS_NETWORK_STATE)权限uses-permissionandroid:nameandroid.permission.INTERNET/uses-permissionandroid:nameandroid.permission.ACCESS_NETWORK_STATE/依赖配置config.json{minSdkVersion:21,dependencies:[{id:org.jetbrains.kotlin:kotlin-stdlib,version:1.8.22}],abiFilters:[arm64-v8a,armeabi-v7a]}build.gradleAndroid 工程侧plugins { id com.android.library id org.jetbrains.kotlin.android } android { namespace com.sl.sl_uniplugin_tcp_udp compileSdk 35 defaultConfig { minSdk 21 consumerProguardFiles consumer-rules.pro } compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } kotlinOptions { jvmTarget 11 } } dependencies { implementation androidx.core:core-ktx:1.10.1 implementation androidx.appcompat:appcompat:1.6.1 // uts 插件运行时依赖io.dcloud.uts.* / io.dcloud.uniapp.* compileOnly fileTree(dir: ${rootProject.projectDir}/simpleDemo/libs, include: [*.aar, *.jar]) }proguard-rules.pro / consumer-rules.pro# 保留 UTSJSONObject 序列化 -keep class io.dcloud.uts.UTSJSONObject { *; }APITCP 客户端tcpConnect(options, callback)连接 TCP 服务端。在子线程执行不阻塞 UI。参数类型必填说明options.hostString是服务端 IP 地址options.portNumber是服务端端口options.timeoutNumber否连接超时毫秒默认 5000回调返回值// 成功{code:0,success:true,msg:Connected}// 失败{code:1,success:false,msg:Connect failed: ...}示例import{tcpConnect}from/uni_modules/sl-uniplugin-tcp-udptcpConnect({host:192.168.1.100,port:8080,timeout:5000},(res){if(res.code0){console.log(TCP 已连接)}else{console.error(连接失败,res.msg)}})tcpSend(options, callback)通过已建立的 TCP 连接发送数据UTF-8 编码支持中文。参数类型必填说明options.dataString是要发送的文本数据示例import{tcpSend}from/uni_modules/sl-uniplugin-tcp-udptcpSend({data:你好 TCP},(res){if(res.code0){console.log(发送成功)}else{console.error(发送失败,res.msg)}})tcpClose(options, callback)关闭 TCP 连接释放 Socket 和流资源。示例import{tcpClose}from/uni_modules/sl-uniplugin-tcp-udptcpClose({},(res){console.log(TCP 已关闭)})tcpIsConnected()返回 TCP 是否处于已连接状态同步。import{tcpIsConnected}from/uni_modules/sl-uniplugin-tcp-udpconstconnectedtcpIsConnected()tcpGetReceivedData()获取 TCP 最新接收数据前端轮询。由于 UTS callback 一次性限制接收数据通过此方法轮询获取。返回值{code:0,success:true,type:tcp,data:收到的文本,from:server,timestamp:1234567890123}轮询示例import{tcpGetReceivedData}from/uni_modules/sl-uniplugin-tcp-udpletlastTs0setInterval((){constrestcpGetReceivedData()if(resres.timestamplastTs){lastTsres.timestamp console.log(TCP 收到,res.data)}},200)TCP 服务端tcpStartServer(options, callback)启动 TCP 服务端监听指定端口。在子线程执行不阻塞 UI。参数类型必填说明options.portNumber是监听端口示例import{tcpStartServer}from/uni_modules/sl-uniplugin-tcp-udptcpStartServer({port:9090},(res){if(res.code0){console.log(服务端已启动)}})tcpServerSend(options, callback)TCP 服务端向已连接的客户端发送数据UTF-8 编码支持中文。需要有客户端已连接才能发送。参数类型必填说明options.dataString是要发送的文本数据示例import{tcpServerSend}from/uni_modules/sl-uniplugin-tcp-udptcpServerSend({data:你好来自服务端},(res){if(res.code0){console.log(服务端发送成功)}else{console.error(发送失败,res.msg)}})tcpStopServer(options, callback)停止 TCP 服务端关闭 ServerSocket 和所有客户端连接。示例import{tcpStopServer}from/uni_modules/sl-uniplugin-tcp-udptcpStopServer({},(res){console.log(服务端已停止)})tcpServerIsRunning()TCP 服务端是否运行中同步。import{tcpServerIsRunning}from/uni_modules/sl-uniplugin-tcp-udpconstrunningtcpServerIsRunning()tcpServerGetReceivedData()获取 TCP 服务端最新接收数据前端轮询。返回值{code:0,success:true,type:tcp-server,data:客户端发来的文本,from:192.168.1.50:12345,timestamp:1234567890123}UDPudpSend(options, callback)发送 UDP 数据包UTF-8 编码支持中文。参数类型必填说明options.hostString是目标 IP 地址options.portNumber是目标端口options.dataString是要发送的文本数据示例import{udpSend}from/uni_modules/sl-uniplugin-tcp-udpudpSend({host:192.168.1.100,port:8080,data:你好 UDP},(res){if(res.code0){console.log(UDP 发送成功)}})udpStartReceive(options, callback)启动 UDP 接收监听指定端口。在子线程执行不阻塞 UI。参数类型必填说明options.portNumber是监听端口示例import{udpStartReceive}from/uni_modules/sl-uniplugin-tcp-udpudpStartReceive({port:9090},(res){if(res.code0){console.log(UDP 接收已启动)}})udpStopReceive(options, callback)停止 UDP 接收关闭 DatagramSocket。示例import{udpStopReceive}from/uni_modules/sl-uniplugin-tcp-udpudpStopReceive({},(res){console.log(UDP 接收已停止)})udpIsReceiving()UDP 是否接收中同步。import{udpIsReceiving}from/uni_modules/sl-uniplugin-tcp-udpconstreceivingudpIsReceiving()udpGetReceivedData()获取 UDP 最新接收数据前端轮询。返回值{code:0,success:true,type:udp,data:收到的文本,from:192.168.1.50:12345,timestamp:1234567890123}技术细节子线程执行连接、发送、接收均在子线程执行不阻塞 UIUTF-8 编解码所有文本数据使用 UTF-8 编解码支持中文等多字节字符编码str.toByteArray(Charset.UTF_8)Kotlin 扩展方法非 JavagetBytes()解码bytes.toString(Charset.UTF_8)Kotlin 扩展方法非new String(bytes, charset)轮询接收由于 UTS callback 在 Weex 引擎下只能被 JS 端接收一次接收数据通过getReceivedData()方法轮询获取用timestamp字段判断新数据Runnable 语法UTS 中用new class implements Runnable { override run(): void {} }不支持new Runnable({ run: () {} })和 Kotlin 的object : Runnable {}语法ByteArrayUTS 中用 Kotlin 原生ByteArray类型数组大小用.size属性子数组用.copyOfRange(from, to)TCP 服务端客户端管理当前实现保存最后连接的客户端输出流tcpServerSend向该客户端发送数据客户端断开时自动清理引用UTS 开发注意事项minSdkVersion 21Android 5.0网络权限需要在AndroidManifest.xml中声明INTERNET和ACCESS_NETWORK_STATE权限不要在主线程做网络操作Android 禁止在主线程进行网络请求本插件已自动在子线程执行轮询间隔建议轮询间隔 200-300ms页面卸载时需清除定时器端口占用UDP 接收端口和 TCP 服务端端口不能被其他应用占用数据格式当前仅支持 UTF-8 文本数据二进制数据需自行处理 Base64 编解码不支持import ... asUTS 不支持import String as JString from java.lang.String语法java.lang.String在 Kotlin 环境中默认可用不支持getBytes()UTS 的string映射到 KotlinString需用toByteArray()扩展方法UTS callback 一次性限制在 uni-app Vue3非 uni-app x项目 App-Android 真机环境下UTS callback 只能被 JS 端接收一次。本插件的接收数据通过轮询getReceivedData()获取不依赖多次 callback