feat:修改
This commit is contained in:
parent
b80d575b96
commit
738e83c356
@ -2,19 +2,19 @@ import socket from '@ohos.net.socket'
|
|||||||
import util from '@ohos.util'
|
import util from '@ohos.util'
|
||||||
import promptAction from '@ohos.promptAction'
|
import promptAction from '@ohos.promptAction'
|
||||||
import TcpToByte from './utils/tcp2byte'
|
import TcpToByte from './utils/tcp2byte'
|
||||||
import { bytesToDecimal } from './utils/tools'
|
import {bytesToDecimal} from './utils/tools'
|
||||||
|
|
||||||
const TAG = '[TCP2BYTE]'
|
const TAG = '[TCP2BYTE]'
|
||||||
|
|
||||||
|
|
||||||
interface RES {
|
interface RES {
|
||||||
code: number | string,
|
code: number|string,
|
||||||
message?: string
|
message?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
address: '172.37.55.191',
|
address: '114.55.125.222',
|
||||||
port: 40000
|
port: 50189
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function tcp2ByteRequest(data): Promise<RES> {
|
export default async function tcp2ByteRequest(data): Promise<RES> {
|
||||||
@ -28,11 +28,7 @@ export default async function tcp2ByteRequest(data): Promise<RES> {
|
|||||||
console.info(TAG, 'sendData=>' + JSON.stringify(sendData))
|
console.info(TAG, 'sendData=>' + JSON.stringify(sendData))
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await tcpClient.connect({
|
await tcpClient.connect({address: {address, port}})
|
||||||
address: {
|
|
||||||
address, port
|
|
||||||
}
|
|
||||||
})
|
|
||||||
//发送消息
|
//发送消息
|
||||||
handSendMessage(tcpClient, data.sjbs, sendData)
|
handSendMessage(tcpClient, data.sjbs, sendData)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -47,7 +43,7 @@ export default async function tcp2ByteRequest(data): Promise<RES> {
|
|||||||
//收到消息
|
//收到消息
|
||||||
tcpClient.on('message', (data) => {
|
tcpClient.on('message', (data) => {
|
||||||
const res = handReceiveMessage(tcpClient, sendData, data.message)
|
const res = handReceiveMessage(tcpClient, sendData, data.message)
|
||||||
if (res) {
|
if(res){
|
||||||
tcpClient.close()
|
tcpClient.close()
|
||||||
resolve(res)
|
resolve(res)
|
||||||
}
|
}
|
||||||
@ -78,32 +74,32 @@ function handSendMessage(client: socket.TCPSocket, type, data) {
|
|||||||
case '02-21-000012':
|
case '02-21-000012':
|
||||||
case '02-21-000014':
|
case '02-21-000014':
|
||||||
data.forEach((item) => {
|
data.forEach((item) => {
|
||||||
console.log(TAG, JSON.stringify(item))
|
client.send({ data: new Uint8Array(item).buffer })
|
||||||
client.send({ data: new Uint8Array(item).buffer })
|
|
||||||
})
|
})
|
||||||
break;
|
break;
|
||||||
case '02-21-000010':
|
case '02-21-000010':
|
||||||
case '02-21-000011':
|
case '02-21-000011':
|
||||||
case '02-21-000013':
|
case '02-21-000013':
|
||||||
client.send({ data: new Uint8Array(data).buffer })
|
client.send({ data: new Uint8Array(data).buffer })
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//处理接收的数据
|
//处理接收的数据
|
||||||
function handReceiveMessage(client: socket.TCPSocket, sendData, rData: ArrayBuffer): RES {
|
function handReceiveMessage(client: socket.TCPSocket, sendData, rData:ArrayBuffer):RES {
|
||||||
|
|
||||||
const receiveData = new Uint8Array(rData)
|
const receiveData = new Uint8Array(rData)
|
||||||
|
console.info(TAG, 'receiveData=>' + JSON.stringify(receiveData))
|
||||||
//返回的消息类型
|
//返回的消息类型
|
||||||
const messageType = receiveData[1];
|
const messageType = receiveData[1];
|
||||||
|
console.info(TAG, 'receiveData messageType=>' + JSON.stringify(receiveData))
|
||||||
|
|
||||||
//流水号
|
//流水号
|
||||||
const lsh = bytesToDecimal([receiveData[2], receiveData[3]]);
|
const lsh = bytesToDecimal([receiveData[2],receiveData[3]]);
|
||||||
|
|
||||||
//开始补包
|
//开始补包
|
||||||
if (messageType === 0xF0) {
|
if(messageType === 0xF0){
|
||||||
//获取消息体长度
|
//获取消息体长度
|
||||||
const messageLength = receiveData[13]
|
const messageLength = receiveData[13]
|
||||||
//分包总数
|
//分包总数
|
||||||
@ -114,21 +110,21 @@ function handReceiveMessage(client: socket.TCPSocket, sendData, rData: ArrayBuff
|
|||||||
const start = index * 2;
|
const start = index * 2;
|
||||||
const end = start + 1;
|
const end = start + 1;
|
||||||
const packageIndex = bytesToDecimal([packages[start],packages[end]]);
|
const packageIndex = bytesToDecimal([packages[start],packages[end]]);
|
||||||
console.info(TAG, '补包内容' + JSON.stringify(new Uint8Array(sendData[packageIndex-1])))
|
console.info(TAG, '补包内容' + JSON.stringify(new Uint8Array(sendData[packageIndex -1])))
|
||||||
client.send({data:new Uint8Array(sendData[packageIndex-1]).buffer});
|
client.send({data:new Uint8Array(sendData[packageIndex -1]).buffer});
|
||||||
})
|
})
|
||||||
} else {
|
}else{
|
||||||
const decoder = util.TextDecoder.create('utf-8');
|
const decoder = util.TextDecoder.create('utf-8');
|
||||||
const messageLength = bytesToDecimal([receiveData[9], receiveData[10]]);
|
const messageLength = bytesToDecimal([receiveData[9],receiveData[10]]);
|
||||||
const markLength = receiveData[11];
|
const markLength = receiveData[11];
|
||||||
const markContent = decoder.decodeWithStream(receiveData.slice(12, 12 + markLength));
|
const markContent = decoder.decodeWithStream(receiveData.slice(12, 12 + markLength ));
|
||||||
console.info(TAG, 'markContent=>' + markContent)
|
console.info(TAG + messageType, 'markContent=>' + markContent)
|
||||||
const tipLength = receiveData[13];
|
const tipLength = receiveData[13];
|
||||||
const messageContent = decoder.decodeWithStream(receiveData.slice(13 + markLength, 13 + markLength + tipLength));
|
const messageContent = decoder.decodeWithStream(receiveData.slice(13 + markLength, 13 + markLength + tipLength));
|
||||||
console.info(TAG, 'messageContent=>' + messageContent)
|
console.info(TAG + messageType, 'messageContent=>' + messageContent)
|
||||||
return {
|
return {
|
||||||
code: markContent,
|
code:markContent,
|
||||||
message: messageContent
|
message:messageContent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user