diff --git a/entry/src/main/ets/common/utils/GlobalTcp.ts b/entry/src/main/ets/common/utils/GlobalTcp.ts index a96edf76..a709aed5 100644 --- a/entry/src/main/ets/common/utils/GlobalTcp.ts +++ b/entry/src/main/ets/common/utils/GlobalTcp.ts @@ -14,7 +14,16 @@ export async function getTCP() { await globalThis.TcpClient.rebindTcp(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort) await globalThis.TcpClient.connectTcp() await globalThis.TcpClient.sendMsg('1002')//1002 - globalThis.TcpClient.onError() + globalThis.TcpClient.onError((val)=>{ + setTimeout(()=>{ + globalThis.TcpClient.sendMsg('1002')//1002 + if(val){ + // const msg=val.substring(5,val.length-1) + console.log('socketTag[PLC.UdpClient] status:',globalThis.udpClient.getStatus()) + globalThis.udpClient?.sendMsg(val) + } + },1000) + }) await globalThis.TcpClient.onMessage((val)=>{ setTimeout(()=>{ globalThis.TcpClient.sendMsg('1002')//1002 @@ -35,14 +44,20 @@ export async function getTCP() { await globalThis.TcpClient.bindTcp() await globalThis.TcpClient.connectTcp() await globalThis.TcpClient.sendMsg('1002')//1002 - globalThis.TcpClient.onError() + globalThis.TcpClient.onError((val)=>{ + hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val)); + setTimeout(()=>{ + globalThis.TcpClient.sendMsg('1002')//1002 + if(val&& globalThis.udpClient?.sendMsg){ + globalThis.udpClient?.sendMsg(val) + } + },1000) + }) await globalThis.TcpClient.onMessage((val)=>{ hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val)); setTimeout(()=>{ globalThis.TcpClient.sendMsg('1002')//1002 if(val&& globalThis.udpClient?.sendMsg){ - // const msg=val.substring(5,val.length-1) - globalThis.udpClient?.sendMsg(val) } },1000) diff --git a/entry/src/main/ets/common/utils/TcpClient.ts b/entry/src/main/ets/common/utils/TcpClient.ts index 171a96f9..94b69093 100644 --- a/entry/src/main/ets/common/utils/TcpClient.ts +++ b/entry/src/main/ets/common/utils/TcpClient.ts @@ -24,7 +24,11 @@ export default class TcpClient { onError(callback?){ this.tcp.on('error', err => { console.log(TAG,'tcpOnerror', JSON.stringify(err)) - callback&&callback() + setTimeout(async ()=>{ + await this.bindTcp() + await this.connectTcp() + this.onMessage(callback) + },2000) // this.closeUdp(()=>{ // this.bindUdp() // }) @@ -107,12 +111,13 @@ export default class TcpClient { resolve(true) }).catch(err => { globalThis.getCloseTcp=true - + setTimeout(()=>{ + this.connectTcp() + },2000) console.log('testTagconnect,error') - resolve(false) + }); }) - } sendMsg(msg: string) {