Compare commits
	
		
			8 Commits
		
	
	
		
			6a2fb2e902
			...
			9ea8e4e3e7
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					9ea8e4e3e7 | ||
| 
						 | 
					27db86cf2a | ||
| 
						 | 
					b612c376ee | ||
| 
						 | 
					284532f979 | ||
| 
						 | 
					4ed37ae613 | ||
| 
						 | 
					1b2181adcc | ||
| 
						 | 
					ee79034ddf | ||
| 
						 | 
					488e237b6f | 
@ -3,6 +3,7 @@ import util from '@ohos.util'
 | 
			
		||||
import promptAction from '@ohos.promptAction'
 | 
			
		||||
import TcpToByte from './utils/tcp2byte'
 | 
			
		||||
import { bytesToDecimal } from './utils/tools'
 | 
			
		||||
 | 
			
		||||
const TAG = '[TCP2BYTE]'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -13,12 +14,13 @@ interface RES {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const config = {
 | 
			
		||||
  address: '114.55.125.222',
 | 
			
		||||
  port: 50189
 | 
			
		||||
  address: '172.37.55.191',
 | 
			
		||||
  port: 40000
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const singleTcpClient = (function () {
 | 
			
		||||
  let instance;
 | 
			
		||||
 | 
			
		||||
  function createInstance() {
 | 
			
		||||
    return socket.constructTCPSocketInstance()
 | 
			
		||||
  }
 | 
			
		||||
@ -34,51 +36,105 @@ const singleTcpClient = (function () {
 | 
			
		||||
})();
 | 
			
		||||
 | 
			
		||||
export default async function tcp2ByteRequest(data): Promise<RES> {
 | 
			
		||||
 | 
			
		||||
  return new Promise(async (resolve, reject) => {
 | 
			
		||||
    const tcpClient: socket.TCPSocket = singleTcpClient.getClient()
 | 
			
		||||
    const tcpClient: socket.TCPSocket = singleTcpClient.getClient();
 | 
			
		||||
    const { address, port } = config;
 | 
			
		||||
    const tcp2Byte = new TcpToByte();
 | 
			
		||||
    const sendData = tcp2Byte.getRequest(data);
 | 
			
		||||
    console.info(TAG, 'sendData=>' + JSON.stringify(sendData));
 | 
			
		||||
 | 
			
		||||
    const {address,port} = config
 | 
			
		||||
    const tcp2Byte = new TcpToByte()
 | 
			
		||||
    const sendData = tcp2Byte.getRequest(data)
 | 
			
		||||
    console.info(TAG, 'sendData=>' + JSON.stringify(sendData))
 | 
			
		||||
    // 设置超时时间(例如 10 秒)
 | 
			
		||||
    const timeoutDuration = 15000; // 10 秒
 | 
			
		||||
    let timeoutHandle: number | null = null;
 | 
			
		||||
 | 
			
		||||
    // 超时处理函数
 | 
			
		||||
    const timeoutPromise = new Promise<RES>((_, rejectTimeout) => {
 | 
			
		||||
      timeoutHandle = setTimeout(() => {
 | 
			
		||||
        const errMsg = 'TCP request timed out';
 | 
			
		||||
        console.log(TAG, "超时", errMsg);
 | 
			
		||||
        promptAction.showToast({
 | 
			
		||||
          message: errMsg,
 | 
			
		||||
          duration: 3000,
 | 
			
		||||
        });
 | 
			
		||||
        tcpClient.close();
 | 
			
		||||
        rejectTimeout(new Error(errMsg));
 | 
			
		||||
      }, timeoutDuration);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      await tcpClient.connect({address: {address, port}})
 | 
			
		||||
      await tcpClient.connect({
 | 
			
		||||
        address: {
 | 
			
		||||
          address,
 | 
			
		||||
          port,
 | 
			
		||||
        },
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      // 发送消息
 | 
			
		||||
      handSendMessage(tcpClient, data.sjbs, sendData)
 | 
			
		||||
      handSendMessage(tcpClient, data.sjbs, sendData);
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      console.log(TAG, 'tcp client connect error' + JSON.stringify(e))
 | 
			
		||||
      console.log(TAG, 'tcp client connect error' + JSON.stringify(e));
 | 
			
		||||
      promptAction.showToast({
 | 
			
		||||
        message: 'tcp client connect error' + JSON.stringify(e),
 | 
			
		||||
        duration: 3000
 | 
			
		||||
        duration: 3000,
 | 
			
		||||
      });
 | 
			
		||||
      reject(e)
 | 
			
		||||
      reject(e);
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 收到消息
 | 
			
		||||
    tcpClient.on('message', (data) => {
 | 
			
		||||
      const res = handReceiveMessage(tcpClient, sendData, data.message)
 | 
			
		||||
      const res = handReceiveMessage(tcpClient, sendData, data.message);
 | 
			
		||||
      if (res) {
 | 
			
		||||
        tcpClient.close()
 | 
			
		||||
        resolve(res)
 | 
			
		||||
        if (timeoutHandle) clearTimeout(timeoutHandle); // 清除超时
 | 
			
		||||
        tcpClient.close();
 | 
			
		||||
        resolve(res);
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    tcpClient.on('error', (e) => {
 | 
			
		||||
      const errMsg = 'tcp client receive error' + JSON.stringify(e)
 | 
			
		||||
      const errMsg = 'tcp client receive error' + JSON.stringify(e);
 | 
			
		||||
      promptAction.showToast({
 | 
			
		||||
        message: 'tcp client receive error' + JSON.stringify(e),
 | 
			
		||||
        duration: 3000
 | 
			
		||||
        duration: 3000,
 | 
			
		||||
      });
 | 
			
		||||
      console.log(TAG, errMsg)
 | 
			
		||||
      tcpClient.close()
 | 
			
		||||
      console.log(TAG, errMsg);
 | 
			
		||||
      if (timeoutHandle) clearTimeout(timeoutHandle); // 清除超时
 | 
			
		||||
      tcpClient.close();
 | 
			
		||||
      resolve({
 | 
			
		||||
        code: -1, message: errMsg
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
        code: 2300028,
 | 
			
		||||
        message: errMsg,
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    // 使用 Promise.race 处理超时和 TCP 请求
 | 
			
		||||
    Promise.race([timeoutPromise, new Promise<RES>((resolveMain) => {
 | 
			
		||||
      // 将 resolve 逻辑移到此处
 | 
			
		||||
      tcpClient.on('message', (data) => {
 | 
			
		||||
        const res = handReceiveMessage(tcpClient, sendData, data.message);
 | 
			
		||||
        if (res) {
 | 
			
		||||
          resolveMain(res);
 | 
			
		||||
        }
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      tcpClient.on('error', (e) => {
 | 
			
		||||
        const errMsg = 'tcp client receive error' + JSON.stringify(e);
 | 
			
		||||
        resolveMain({
 | 
			
		||||
          code: 2300028,
 | 
			
		||||
          message: errMsg,
 | 
			
		||||
        });
 | 
			
		||||
      });
 | 
			
		||||
    })])
 | 
			
		||||
      .then((result) => {
 | 
			
		||||
        if (timeoutHandle) clearTimeout(timeoutHandle); // 清除超时
 | 
			
		||||
        tcpClient.close();
 | 
			
		||||
        resolve(result);
 | 
			
		||||
      })
 | 
			
		||||
      .catch((error) => {
 | 
			
		||||
        if (timeoutHandle) clearTimeout(timeoutHandle); // 清除超时
 | 
			
		||||
        tcpClient.close();
 | 
			
		||||
        reject(error);
 | 
			
		||||
      });
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//处理发送的数据
 | 
			
		||||
@ -89,7 +145,8 @@ function handSendMessage(client: socket.TCPSocket, type, data) {
 | 
			
		||||
    case '02-21-000009':
 | 
			
		||||
    case '02-21-000012':
 | 
			
		||||
    case '02-21-000014':
 | 
			
		||||
      data.forEach((item) => {
 | 
			
		||||
      data.forEach((item, index) => {
 | 
			
		||||
        console.log(TAG, "分包", index.toString(), JSON.stringify(item))
 | 
			
		||||
        client.send({ data: new Uint8Array(item).buffer })
 | 
			
		||||
      })
 | 
			
		||||
      break;
 | 
			
		||||
@ -98,8 +155,8 @@ function handSendMessage(client: socket.TCPSocket, type, data) {
 | 
			
		||||
    case '02-21-000013':
 | 
			
		||||
      client.send({ data: new Uint8Array(data).buffer })
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    default:break;
 | 
			
		||||
    default:
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -127,6 +184,7 @@ function handReceiveMessage(client: socket.TCPSocket, sendData, rData:ArrayBuffe
 | 
			
		||||
      const end = start + 1;
 | 
			
		||||
      const packageIndex = bytesToDecimal([packages[start], packages[end]]);
 | 
			
		||||
      console.info(TAG, '补包内容' + JSON.stringify(new Uint8Array(sendData[packageIndex -1])))
 | 
			
		||||
      printInBatches(new Uint8Array(sendData[packageIndex -1]), 100)
 | 
			
		||||
      client.send({ data: new Uint8Array(sendData[packageIndex -1]).buffer });
 | 
			
		||||
    })
 | 
			
		||||
  } else {
 | 
			
		||||
@ -144,3 +202,10 @@ function handReceiveMessage(client: socket.TCPSocket, sendData, rData:ArrayBuffe
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function printInBatches(array, batchSize) {
 | 
			
		||||
  for (let i = 0; i < array.length; i += batchSize) {
 | 
			
		||||
    const batch = array.slice(i, i + batchSize);
 | 
			
		||||
    console.log(TAG, "补包细分", batch);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user