126 lines
3.6 KiB
TypeScript
126 lines
3.6 KiB
TypeScript
|
|
import socket from '@ohos.net.socket'
|
||
|
|
import util from '@ohos.util'
|
||
|
|
import promptAction from '@ohos.promptAction'
|
||
|
|
import TcpToByte from './utils/tcp2byte'
|
||
|
|
import {bytesToDecimal} from './utils/tools'
|
||
|
|
const TAG = '[TCP2BYTE]'
|
||
|
|
|
||
|
|
|
||
|
|
interface RES {
|
||
|
|
code: number|string,
|
||
|
|
message?: string
|
||
|
|
}
|
||
|
|
|
||
|
|
const config = {
|
||
|
|
address: '114.55.125.222',
|
||
|
|
port: 50189
|
||
|
|
}
|
||
|
|
|
||
|
|
export default async function tcp2ByteRequest(data): Promise<RES> {
|
||
|
|
|
||
|
|
return new Promise(async (resolve, reject) => {
|
||
|
|
const tcpClient: socket.TCPSocket = socket.constructTCPSocketInstance()
|
||
|
|
|
||
|
|
const {address,port} = config
|
||
|
|
const tcp2Byte = new TcpToByte()
|
||
|
|
const sendData = tcp2Byte.getRequest(data)
|
||
|
|
console.info(TAG, 'sendData=>' + JSON.stringify(sendData))
|
||
|
|
|
||
|
|
try {
|
||
|
|
await tcpClient.connect({address: {address, port}})
|
||
|
|
//发送消息
|
||
|
|
handSendMessage(tcpClient, data.sjbs, sendData)
|
||
|
|
} catch (e) {
|
||
|
|
console.log(TAG, 'tcp client connect error' + JSON.stringify(e))
|
||
|
|
promptAction.showToast({
|
||
|
|
message: 'tcp client connect error' + JSON.stringify(e),
|
||
|
|
duration: 3000
|
||
|
|
});
|
||
|
|
reject(e)
|
||
|
|
}
|
||
|
|
|
||
|
|
//收到消息
|
||
|
|
tcpClient.on('message', (data) => {
|
||
|
|
const res = handReceiveMessage(tcpClient, sendData, data)
|
||
|
|
if(res){
|
||
|
|
tcpClient.close()
|
||
|
|
resolve(res)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
tcpClient.on('error', (e) => {
|
||
|
|
const errMsg = 'tcp client receive error' + JSON.stringify(e)
|
||
|
|
promptAction.showToast({
|
||
|
|
message: 'tcp client receive error' + JSON.stringify(e),
|
||
|
|
duration: 3000
|
||
|
|
});
|
||
|
|
console.log(TAG, errMsg)
|
||
|
|
tcpClient.close()
|
||
|
|
resolve({
|
||
|
|
code: -1, message: errMsg
|
||
|
|
})
|
||
|
|
})
|
||
|
|
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
//处理发送的数据
|
||
|
|
function handSendMessage(client: socket.TCPSocket, type, data) {
|
||
|
|
|
||
|
|
switch (type) {
|
||
|
|
//开始考试 过程照片 考试结束需要分包
|
||
|
|
case '02-21-000009':
|
||
|
|
case '02-21-000012':
|
||
|
|
case '02-21-000014':
|
||
|
|
data.forEach((item) => {
|
||
|
|
client.send({ data: new Uint8Array(item) })
|
||
|
|
})
|
||
|
|
break;
|
||
|
|
case '02-21-000010':
|
||
|
|
case '02-21-000011':
|
||
|
|
case '02-21-000013':
|
||
|
|
client.send({ data: new Uint8Array(data) })
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//处理接收的数据
|
||
|
|
function handReceiveMessage(client: socket.TCPSocket, sendData, receiveData):RES {
|
||
|
|
console.info(TAG, 'receiveData=>' + JSON.stringify(receiveData))
|
||
|
|
//返回的消息类型
|
||
|
|
const messageType = receiveData[1];
|
||
|
|
//流水号
|
||
|
|
const lsh = bytesToDecimal([receiveData[2],receiveData[3]]);
|
||
|
|
|
||
|
|
//开始补包
|
||
|
|
if(messageType === 0xF0){
|
||
|
|
//获取消息体长度
|
||
|
|
const messageLength = receiveData[12]
|
||
|
|
//分包总数
|
||
|
|
const packages = receiveData.slice(13,13 + messageLength*2);
|
||
|
|
const forArr = new Array(packages).fill(1)
|
||
|
|
|
||
|
|
forArr.forEach((item,index)=>{
|
||
|
|
const start = index * 2;
|
||
|
|
const end = start + 2;
|
||
|
|
const packageIndex = bytesToDecimal([start,end]);
|
||
|
|
console.info(TAG, '补包内容' + JSON.stringify(new Uint8Array(sendData[packageIndex])))
|
||
|
|
client.send({data:new Uint8Array(sendData[packageIndex])});
|
||
|
|
})
|
||
|
|
}else{
|
||
|
|
const decoder = util.TextDecoder.create('utf-8');
|
||
|
|
const messageLength = bytesToDecimal([receiveData[9],receiveData[10]]);
|
||
|
|
const markLength = receiveData[11];
|
||
|
|
const markContent = decoder.decodeWithStream(receiveData.slice(12, 12 + markLength ));
|
||
|
|
console.info(TAG, 'markContent=>' + markContent)
|
||
|
|
const tipLength = receiveData[13];
|
||
|
|
const messageContent = decoder.decodeWithStream(receiveData.slice(13 + markLength, 13 + markLength + tipLength));
|
||
|
|
console.info(TAG, 'messageContent=>' + messageContent)
|
||
|
|
return {
|
||
|
|
code:markContent,
|
||
|
|
message:messageContent
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|