更新记录
1.0.5(2025-08-04)
- 修复鸿蒙HarmonyOS端广播功能BUG
1.0.4(2025-08-04)
- 增加鸿蒙HarmonyOS端BUG
- 修复示例项目在iOS端无法显示日志的问题
1.0.3(2025-08-01)
- 增加鸿蒙HarmonyOS支持
平台兼容性
云端兼容性
阿里云 | 腾讯云 | 支付宝云 |
---|---|---|
√ | √ | √ |
uni-app(4.66)
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 | 鸿蒙插件版本 |
---|---|---|---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | 5.0 | 1.0.0 | 13 | 1.0.1 | 12 | 1.0.4 |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | × | × | × | × | × |
uni-app x(4.66)
Chrome | Safari | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 | 鸿蒙插件版本 | 微信小程序 |
---|---|---|---|---|---|---|---|---|
× | × | 5.0 | 1.0.0 | 13 | 1.0.1 | 12 | 1.0.4 | × |
关于版本
【源码授权版】和【普通授权版】功能上是完全一致的。
【源码授权版】适用于对外部插件源代码有要求或者需要基于插件源码进行二次开发和定制的场景。源码授权版支持使用标准基座进行运行调试。
关于插件
本插件为uniapp提供原生TCP通信能力。
- 支持在APP建立TCP服务监听,让外部的各种设备(通常是局域网设备)链接到APP进行通信,适用于把APP当做通信服务主控的场景。
- 支持在APP端建立TCP链接连接到外部的服务器进行通信,支持同时建立多个TCP链接连接到不同的服务器。
API文档
在线的API文档链接:TCP插件API文档
如何使用
引入
import { UtsTcpServerManager, UtsTcpClientManager, UtsErrorMessage } from '@/uni_modules/ts-tcp-tool';
TCP服务器基本使用
使用UtsTcpServerManager
建立TCP服务器监听
let tcpServer = new UtsTcpServerManager()
// 建立服务器监听
tcpServer.createTcpServer(
// 配置
{
listenPort: 18897, // 监听的端口
dataType: "text", // 客户端发送过来的消息编码格式,支持:text-UTF8字符串,hex-十六进制HEX字符串,byte-二进制字节数组
},
// 回调
{
/** 客户端建立连接 */
onConnected(session) {
console.log("客户端已连接:", session)
},
/** 客户端断开链接 */
onDisconnected(session) {
console.log("客户端断开链接:", session)
},
/** 接受到客户端发送过来的数据消息 */
onReceivedData(session, message) {
console.log("接收到客户端发送的消息:", session, message)
// ... 可以在这里基于客户端发送的消息进行各种处理...
// 例如:回复给客户端消息
tcpServer.sendMessage({
sessionId: session.sessionId,
dataType: "text",
data: "this is server reply",
})
},
/** 错误消息 */
onErrorStr(errorMsg) {
console.log("TCP错误消息:", errorMsg)
},
},
errMsg => {
console.log("创建TCP服务结果回调:", errMsg)
}
)
给在线客户端发送消息。
需要使用sessionId指定是哪个客户端,这个ID在客户端建立链接的时候会自动生成用于标识一个唯一的客户端
// 支持使用text明文字符串发送消息
tcpServer.sendMessage({
sessionId: "session-1",
dataType: "text",
data: "hello, world",
}, (errMsg : UtsErrorMessage) => {
console.log("回调结果:", errMsg);
})
// 支持使用hex字符串发送消息
tcpServer.sendMessage({
sessionId: "session-1",
dataType: "hex",
data: "68656c6c6f",
}, (errMsg : UtsErrorMessage) => {
console.log("回调结果:", errMsg);
})
// 支持使用byte数组发送消息
tcpServer.sendMessage({
sessionId: "session-1",
dataType: "byte",
byteData: [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x31, 0x32, 0x33],
}, (errMsg : UtsErrorMessage) => {
console.log("回调结果:", errMsg);
})
主动断开客户端链接
tcpServer.removeSession("session-1")
获取当前链接的所有客户端列表
let clientList = tcpServer.connectedSessionList()
console.log("获取所有TCP客户端:", clientList)
TCP客户端基本使用
使用UtsTcpClientManager
建立TCP Socket连接
let tcpClientManager = new UtsTcpClientManager()
// 创建一个TCP Socket连接,多次调用可以创建多个
tcpClientManager.createNewConnection(
"my-tcp-client", // TCP客户端的唯一标识,如果不指定则系统会自动生成一个
{ // 客户端配置
serverAddress: "127.0.0.1",
serverPort: 18897,
dataType: "text", // 接收的数据格式,支持:text-UTF8字符串,hex-十六进制HEX字符串,byte-二进制字节数组
},
{ // 消息回调
onConnectError(id, errorStr) {
console.log(id, "客户端连接错误:", errorStr)
},
onConnected(id) {
console.log(id, "客户端建立连接")
},
onReceivedMessage(message) {
console.log("客户端接收到服务器发送的消息:", message)
},
onDisconnected(id) {
console.log(id, "客户端断开连接")
},
},
errMsg => {
console.log("创建TCP链接结果回调:", errMsg)
}
)
通过指定的客户端发送消息给服务器
// 支持使用text明文字符串发送消息
tcpClientManager.sendMessage({
connectionId: "my-tcp-client",
dataType: "text",
data: "hello!!!!!!!!"
}, errMsg => {
console.log("发送结果回调:", errMsg)
})
// 支持使用hex字符串发送消息
tcpClientManager.sendMessage({
connectionId: "my-tcp-client",
dataType: "hex",
data: "68656c6c6f",
}, errMsg => {
console.log("发送结果回调:", errMsg)
})
// 支持使用byte数组发送消息
tcpClientManager.sendMessage({
connectionId: "my-tcp-client",
dataType: "byte",
byteData: [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x31, 0x32, 0x33],
}, errMsg => {
console.log("发送结果回调:", errMsg)
})
断开客户端链接
tcpClientManager.closeConnection("my-tcp-client")
获取当前所有客户端链接
let connList = tcpClientManager.getAllConnection()
console.log("所有链接信息:", connList)