2024-01-05 11:11:15 +08:00
|
|
|
import socket from '@ohos.net.socket';
|
2024-07-30 10:35:41 +08:00
|
|
|
import hilog from '@ohos.hilog';
|
2024-08-07 08:57:40 +08:00
|
|
|
import { getTCP } from './GlobalTcp';
|
2024-01-05 11:11:15 +08:00
|
|
|
|
2024-07-03 16:19:55 +08:00
|
|
|
const TAG = 'socketTag[TcpDemo.TcpClient]'
|
2024-01-05 11:11:15 +08:00
|
|
|
|
|
|
|
|
export default class TcpClient {
|
2024-07-30 10:35:41 +08:00
|
|
|
private localIp: string = ''
|
|
|
|
|
private localIpPort: string = ''
|
|
|
|
|
private oppositeIp: string = ''
|
|
|
|
|
private oppositeIpPort: string = ''
|
|
|
|
|
|
|
|
|
|
private tcp: any = null
|
|
|
|
|
|
|
|
|
|
constructor(tcplocalIp: string, tcplocalIpPort: string, tcpOppositeIp: string, tcpOppositePort: string) {
|
|
|
|
|
this.localIp = tcplocalIp
|
|
|
|
|
this.oppositeIp = tcpOppositeIp
|
|
|
|
|
this.localIpPort = tcplocalIpPort
|
|
|
|
|
this.oppositeIpPort = tcpOppositePort
|
|
|
|
|
console.log(TAG, 'new Tcp', this.localIp, this.localIpPort, this.oppositeIp, this.oppositeIpPort)
|
|
|
|
|
this.tcp = socket.constructTCPSocketInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onError(callback?) {
|
|
|
|
|
this.tcp.on('error', err => {
|
|
|
|
|
console.log(TAG, 'tcpOnerror', JSON.stringify(err))
|
|
|
|
|
setTimeout(async () => {
|
2024-08-07 08:57:40 +08:00
|
|
|
getTCP()
|
2024-07-30 10:35:41 +08:00
|
|
|
}, 2000)
|
|
|
|
|
// this.closeUdp(()=>{
|
|
|
|
|
// this.bindUdp()
|
|
|
|
|
// })
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rebindTcp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.localIp = localIp
|
|
|
|
|
this.oppositeIp = oppositeIp
|
|
|
|
|
this.localIpPort = localIpPort
|
|
|
|
|
this.oppositeIpPort = oppositeIpPort
|
|
|
|
|
console.log(TAG, 'tcpreBind', this.localIp, this.localIpPort)
|
|
|
|
|
let promise = this.tcp.bind({
|
|
|
|
|
address: this.localIp, port: parseInt(this.localIpPort), family: 1
|
|
|
|
|
}, err => {
|
|
|
|
|
if (err) {
|
|
|
|
|
globalThis.getCloseTcp = true
|
|
|
|
|
hilog.info(0x0000, 'testTag', "tcpreBinderror:" + JSON.stringify(err));
|
|
|
|
|
resolve(true)
|
|
|
|
|
}
|
|
|
|
|
console.log('testTag,rebindtestTag tcp bind success');
|
|
|
|
|
globalThis.getCloseTcp = false
|
|
|
|
|
resolve(false)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// let promise = this.udp.bind({
|
|
|
|
|
// // address: '192.168.7.170', port: 20122, family: 1
|
|
|
|
|
// // address: '192.168.7.170', port: 31013, family: 1
|
|
|
|
|
// address: this.localIp, port: parseInt(this.localIpPort), family: 1
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bindTcp() {
|
|
|
|
|
console.log(TAG, 'tcpbind', this.localIp, 'localIp', this.localIpPort)
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let promise = this.tcp.bind({
|
|
|
|
|
address: this.localIp, port: parseInt(this.localIpPort), family: 1
|
|
|
|
|
}, err => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log('testTag tcp bind faile');
|
|
|
|
|
globalThis.getCloseTcp = true
|
|
|
|
|
hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err));
|
|
|
|
|
resolve(true)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
globalThis.getCloseTcp = false
|
|
|
|
|
console.log('testTag tcp bind success');
|
|
|
|
|
resolve(false)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
console.log('localIp', this.localIp)
|
|
|
|
|
console.log('localIpPort', this.localIpPort)
|
|
|
|
|
// let promise = this.udp.bind({
|
|
|
|
|
// // address: '192.168.7.170', port: 20122, family: 1
|
|
|
|
|
// // address: '192.168.7.170', port: 31013, family: 1
|
|
|
|
|
// address: this.localIp, port: parseInt(this.localIpPort), family: 1
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connectTcp() {
|
|
|
|
|
console.log(TAG, 'tcpConnect', this.oppositeIp, 'localIp', this.oppositeIpPort)
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let promise = this.tcp.connect({
|
|
|
|
|
address: {
|
|
|
|
|
address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1
|
|
|
|
|
}, timeout: 6000
|
|
|
|
|
});
|
|
|
|
|
promise.then(() => {
|
|
|
|
|
|
|
|
|
|
this.tcp.setExtraOptions({
|
|
|
|
|
keepAlive: true,
|
|
|
|
|
// OOBInline: true,
|
|
|
|
|
// TCPNoDelay: true,
|
|
|
|
|
// socketLinger: { on:true, linger:10 },
|
|
|
|
|
// receiveBufferSize: 1000,
|
|
|
|
|
// sendBufferSize: 1000,
|
|
|
|
|
// reuseAddress: true,
|
|
|
|
|
// socketTimeout: 3000,
|
|
|
|
|
}, err => {
|
|
|
|
|
if (err) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-01-05 11:11:15 +08:00
|
|
|
});
|
2024-07-30 10:35:41 +08:00
|
|
|
globalThis.getCloseTcp = false
|
|
|
|
|
resolve(true)
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
globalThis.getCloseTcp = true
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.connectTcp()
|
|
|
|
|
resolve(false)
|
|
|
|
|
}, 2000)
|
|
|
|
|
console.log('testTagconnect,error')
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendMsg(msg: string) {
|
|
|
|
|
console.log(TAG, 'tcpSend', msg.length, msg)
|
|
|
|
|
return new Promise((reslove, reject) => {
|
|
|
|
|
let promise = this.tcp.send({
|
|
|
|
|
data: msg
|
|
|
|
|
});
|
|
|
|
|
promise.then(() => {
|
|
|
|
|
reslove(true)
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
reslove(false)
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onMessage(callback?) {
|
|
|
|
|
this.tcp.on('message', value => {
|
|
|
|
|
console.log(TAG, 'Tcponmessage', value.length, value)
|
|
|
|
|
|
|
|
|
|
// console.log('messageLengt',,value.message.length))
|
|
|
|
|
console.log('testTagtcpmsg')
|
|
|
|
|
// console.log("on message, message:" + value.message+ ", remoteInfo:" )
|
|
|
|
|
if (value) {
|
|
|
|
|
let dataView = new DataView(value.message)
|
|
|
|
|
// const Arraybuffer=buffer.from(value.message, 5, dataView?.byteLength);
|
|
|
|
|
const Arraybuffer = value.message.slice(5, dataView?.byteLength);
|
|
|
|
|
|
|
|
|
|
callback && callback(Arraybuffer)
|
|
|
|
|
} else {
|
|
|
|
|
callback && callback('')
|
|
|
|
|
}
|
|
|
|
|
// callback(value.message)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
offTcp(callback) {
|
|
|
|
|
console.log(TAG, 'tcpofff')
|
|
|
|
|
|
|
|
|
|
this.tcp.off('testTagofmessg', callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closeTcp(callback) {
|
|
|
|
|
return new Promise((reslove, reject) => {
|
|
|
|
|
console.log(TAG, 'tcpClose')
|
|
|
|
|
let promise = this.tcp.close();
|
|
|
|
|
promise.then(() => {
|
|
|
|
|
globalThis.getCloseTcp = true
|
|
|
|
|
console.log(TAG, 'tcpCloseSuccess')
|
|
|
|
|
callback()
|
|
|
|
|
reslove(true)
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.log(TAG, 'tcpClosefailed')
|
|
|
|
|
reslove(false)
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|