更新记录
1.0.0(2025-07-03)
1.0.0 (2025-07-03)
初始版本
平台兼容性
uni-app(4.66)
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
- | - | - | - | - | - | 5.0 | - | - |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
- | - | - | - | - | - | - | - | - | - | - |
uni-app x(4.66)
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | 5.0 | - | - | - |
关于插件
本插件为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", // 客户端发送过来的消息编码格式,支持多种格式
},
// 回调
{
/** 客户端建立连接 */
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)
},
}
)
给在线客户端发送消息。
需要使用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连接,多次调用可以创建多个
let connection = tcpClientManager.createNewConnection(
"my-tcp-client", // TCP客户端的唯一标识,如果不指定则系统会自动生成一个
{ // 客户端配置
serverAddress: "127.0.0.1",
serverPort: 18897,
},
{ // 消息回调
onConnectError(id, errorStr) {
console.log(id, "客户端连接错误:", errorStr)
},
onConnected(id) {
console.log(id, "客户端建立连接")
},
onReceivedMessage(message) {
console.log("客户端接收到服务器发送的消息:", message)
},
onDisconnected(id) {
console.log(id, "客户端断开连接")
},
}
)
通过指定的客户端发送消息给服务器
// 支持使用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)