【源码授权版】和【普通授权版】功能上是完全一致的。
【源码授权版】适用于对外部插件源代码有要求或者需要基于插件源码进行二次开发和定制的场景。源码授权版支持使用标准基座进行运行调试。
本插件为uniapp提供原生TCP通信能力。
在线的API文档链接:TCP插件API文档
import { UtsTcpServerManager, UtsTcpClientManager, UtsErrorMessage } from '@/uni_modules/ts-tcp-tool';
使用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)
使用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)
需要增加以下权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
需要增加以下权限
<key>NSLocalNetworkUsageDescription</key>
<string>需要网络权限来运行TCP网络服务</string>
<!-- 申请更广泛的网络访问权限,本地网络查找 -->
<key>NSAllowsLocalNetworking</key>
<true/>
<!-- 后台网络使用,应用需要在后台运行服务器 -->
<key>UIBackgroundModes</key>
<array>
<string>network</string>
</array>
需要增加以下权限
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
}
]