更新记录
1.3.0(2025-03-10)
- 增加断开连接消息
1.2.0(2025-02-20)
- 增加创建TCP 服务时设备连接消息: createTcpServer() { // var that = this that.$data.msgs.push("click") that.$data.connections.createTcpServer(3000, function(res) { console.log(res) that.$data.msgs.push(res.result) }, function(res) { console.log(res) that.$data.msgs.push(res) }, function(res) { console.log(res) console.log("有新设备连接") that.$data.msgs.push(res) }) }
1.1.0(2024-09-25)
- 增加byte类型收发
- 增加客户端的断线重连机制
- 增加关闭方法
平台兼容性
Android | Android CPU类型 | iOS |
---|---|---|
适用版本区间:5.0 - 13.0 | armeabi-v7a:未测试,arm64-v8a:未测试,x86:未测试 | × |
原生插件通用使用流程:
- 购买插件,选择该插件绑定的项目。
- 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
- 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
- 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
- 开发完毕后正式云打包
付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios
注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择
介绍
Tcp server、client通讯包
使用说明
-
创建插件
that.$data.connections = uni.requireNativePlugin('jjc-tcpTools')
-
创建TCP Server服务
createTcpServer(端口号,回调函数1:创建结果,回调函数2:接收数据)*
数据接收函数2数据说明:
{"ip":"客户端IP","port","客户端端口号","msg","客户端数据"}
that.$data.connections.createTcpServer(3000, function(res) {
console.log(res)
that.$data.msgs.push(res.result)
}, function(res) {
console.log(res)
that.$data.msgs.push(res.msg)
}, function(res) {
console.log("有新设备连接:"+res)
that.$data.msgs.push(res.msg)
})
-
TCP Server群发数据(字符串收发模式)
sendTcpMSG2Client(服务创建时的端口号,回调函数1:发送结果)
that.$data.connections.sendTcpMSG2Client(3000, "要发送的内容\r\n", function(res) {
// res = JSON.parse(res)
console.log(res)
that.$data.msgs.push(res)
})
-
TCP Server向指定IP和端口的客户端发送数据(字符串收发模式)
sendTcpMSG2Clients(服务创建时的端口号,客户端列表,数据内容,回调函数1:发送结果)
that.$data.connections.sendTcpMSG2Clients(3000,"[{"ip":"192.168.1.1","port":555}]", "要发送的内容\r\n", function(res) { // res = JSON.parse(res) console.log(res) that.$data.msgs.push(res) })
-
创建tcp client连接服务器(字符串收发)
createTcpClient(tcpServer地址,tcpServer端口号,回调函数1:创建结果,回调函数2:接收数据) 数据接收函数2数据说明:
{"ip":"客户端IP","port","客户端端口号","msg","客户端数据"}
that.$data.connections.createTcpClient(that.$data.tcpServerIP, 10203, function(res) {
// res = JSON.stringify(res)
console.log(res)
that.$data.msgs.push(res.result)
}, (e) => {
console.log("客户端接收:", e)
that.$data.msgs.push(e)
})
-
tcp client发送数据(字符串收发)
createTcpClient(tcpserver地址,tcpserver端口号,回调函数1:发送结果)
that.$data.connections.sendTcpMSG2Server("192.168.3.78", 10203, "客户端发送", function(res) { console.log(res) that.$data.msgs.push(res.result) }, (e) => { console.log("客户端发送:", e) that.$data.msgs.push(e) })
-
创建tcp server(byte数组收发)
createByteTcpServer(tcpserver端口号,回调函数1:发送结果,回调函数2:接收数据)
示例 that.$data.connections.createByteTcpServer(3000, function(res) { console.log(res) that.$data.msgs.push(res.result) }, function(res) { console.log(res) that.$data.msgs.push(res) })
-
tcp server发送byte(byte数组群发)
sendTcpByteMSG2Client(tcpserver端口号,数据,回调函数1:发送结果)
示例
let msg = [Number(0x55), Number(0x55), Number(0x55), Number(0x55), Number(0x55), Number(0x55)]
that.$data.connections.sendTcpByteMSG2Client(3000, msg, function(res) {
console.log(res)
that.$data.msgs.push(res)
})
-
tcp server发送byte(byte数组指定发)
sendTcpByteMSG2Clients(tcpserver端口号,客户端列表,数据,回调函数1:发送结果)
示例
let msg = [Number(0x55), Number(0x55), Number(0x55), Number(0x55), Number(0x55), Number(0x55)]
that.$data.connections.sendTcpByteMSG2Clients(3000,"[{"ip":"192.168.1.1","port":555}]", msg, function(res) {
console.log(res)
that.$data.msgs.push(res)
})
-
创建tcp client(byte数组收发)
createByteTcpClient(tcpServer地址,tcpServer端口号,回调函数1:创建结果,回调函数2:接收数据)
示例
that.$data.connections.createByteTcpClient(that.$data.tcpServerIP, 10203, function(res) {
// res = JSON.stringify(res)
console.log(res)
that.$data.msgs.push(res.result)
}, (e) => {
console.log("客户端接收:", e)
that.$data.msgs.push(e)
})
-
tcp client发送数据(byte数组发)
sendTcpMSGByte2Server(tcpserver地址,tcpserver端口号,回调函数1:发送结果)
示例
that.$data.connections.sendTcpMSGByte2Server(that.$data.tcpServerIP, 10203, msg, function(res) {
console.log(res)
that.$data.msgs.push(res.result)
}, (e) => {
console.log("客户端接收:", e)
that.$data.msgs.push(e)
})
-
设置客户端重连检测时间
createByteTcpClient(tcpServer地址,tcpServer端口号,重连时间(毫秒),回调函数1:创建结果)
示例
that.$data.connections.setReConnectTime(that.$data.tcpServerIP, 10203,3000, function(res) {
console.log(res)
that.$data.msgs.push(res.result)
})
-
关闭客户端
示例
that.$data.connections.closeTcpClient(that.$data.tcpServerIP, 10203, function(res) { console.log(res) that.$data.msgs.push(res.result) })
-
关闭服务端
示例
that.$data.connections.closeTcpServer(3000, function(res) { console.log(res) that.$data.msgs.push(res.result) })