2024-01-05 11:11:15 +08:00
|
|
|
import socket from '@ohos.net.socket';
|
|
|
|
|
import buffer from '@ohos.buffer';
|
|
|
|
|
|
|
|
|
|
const TAG = '[TcpDemo.TcpClient]'
|
|
|
|
|
import prompt from '@ohos.prompt'
|
2024-05-16 09:53:10 +08:00
|
|
|
import hilog from '@ohos.hilog';
|
2024-01-05 11:11:15 +08:00
|
|
|
|
|
|
|
|
export default class TcpClient {
|
|
|
|
|
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
|
2024-05-16 09:53:10 +08:00
|
|
|
console.log('tcp1111',this.localIp,this.localIpPort,this.oppositeIp,this.oppositeIpPort)
|
2024-01-05 11:11:15 +08:00
|
|
|
this.tcp = socket.constructTCPSocketInstance();
|
|
|
|
|
}
|
2024-05-16 09:53:10 +08:00
|
|
|
onError(callback?){
|
|
|
|
|
this.tcp.on('error', err => {
|
|
|
|
|
hilog.info(0x0000, 'testTag', "on error, err:" + JSON.stringify(err));
|
2024-05-27 17:25:20 +08:00
|
|
|
callback()
|
2024-05-16 09:53:10 +08:00
|
|
|
// this.closeUdp(()=>{
|
|
|
|
|
// this.bindUdp()
|
|
|
|
|
// })
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-01-05 11:11:15 +08:00
|
|
|
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
|
|
|
|
|
let promise=this.tcp.bind({ address: this.localIp, port:parseInt(this.localIpPort), family: 1 }, err => {
|
|
|
|
|
if (err) {
|
2024-05-16 09:53:10 +08:00
|
|
|
hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err));
|
2024-01-05 11:11:15 +08:00
|
|
|
resolve(true)
|
|
|
|
|
}
|
|
|
|
|
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() {
|
2024-05-27 17:25:20 +08:00
|
|
|
console.log('tcpoppositeIp',this.oppositeIp,'localIp',this.localIp)
|
2024-01-05 11:11:15 +08:00
|
|
|
return new Promise((resolve,reject)=>{
|
|
|
|
|
let promise=this.tcp.bind({ address: this.localIp, port:parseInt(this.localIpPort), family: 1 }, err => {
|
|
|
|
|
if (err) {
|
2024-05-27 17:25:20 +08:00
|
|
|
console.log('testTag tcp bind faile');
|
|
|
|
|
|
2024-05-16 09:53:10 +08:00
|
|
|
hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err));
|
2024-01-05 11:11:15 +08:00
|
|
|
resolve(true)
|
2024-05-27 17:25:20 +08:00
|
|
|
return
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
2024-05-27 17:25:20 +08:00
|
|
|
console.log('testTag tcp bind success');
|
2024-01-05 11:11:15 +08:00
|
|
|
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(){
|
2024-05-27 17:25:20 +08:00
|
|
|
console.log('testTagtcpoppositeIp',this.oppositeIp,'localIp',this.localIp)
|
2024-01-05 11:11:15 +08:00
|
|
|
console.log('22323')
|
|
|
|
|
return new Promise((resolve,reject)=>{
|
|
|
|
|
let promise = this.tcp.connect({ address: {address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1} , timeout: 6000});
|
|
|
|
|
promise.then(() => {
|
2024-05-27 17:25:20 +08:00
|
|
|
console.log('testTagbindSuccess')
|
|
|
|
|
|
2024-01-05 11:11:15 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
resolve(true)
|
|
|
|
|
}).catch(err => {
|
2024-05-27 17:25:20 +08:00
|
|
|
console.log('testTagconnect,error')
|
2024-01-05 11:11:15 +08:00
|
|
|
resolve(false)
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 17:25:20 +08:00
|
|
|
sendMsg(msg: string) {
|
|
|
|
|
console.log('oppositeIp',this.oppositeIp)
|
2024-01-05 11:11:15 +08:00
|
|
|
return new Promise((reslove,reject)=>{
|
|
|
|
|
let promise = this.tcp.send({
|
|
|
|
|
data:msg
|
|
|
|
|
});
|
|
|
|
|
promise.then(() => {
|
|
|
|
|
reslove(true)
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
reslove(false)
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// let promise = this.udp.send({
|
|
|
|
|
// data: msg,
|
|
|
|
|
// address: {
|
|
|
|
|
// // address: '192.168.7.124',
|
|
|
|
|
// // port: 30013,
|
|
|
|
|
// // address: '192.168.7.124',
|
|
|
|
|
// // port: 20022,
|
|
|
|
|
// address: this.oppositeIp,
|
|
|
|
|
// port: parseInt(this.oppositeIpPort),
|
|
|
|
|
// family: 1
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// promise.then(() => {
|
|
|
|
|
// console.log(`${TAG} udp send success:${msg}`);
|
|
|
|
|
// }).catch(err => {
|
|
|
|
|
// console.log(`${TAG} udp send fail:${JSON.stringify(err)}`);
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 17:25:20 +08:00
|
|
|
onMessage(callback?) {
|
2024-01-05 11:11:15 +08:00
|
|
|
this.tcp.on('message', value => {
|
|
|
|
|
// console.log('messageLengt',,value.message.length))
|
2024-05-27 17:25:20 +08:00
|
|
|
console.log('tcpmsg')
|
2024-01-05 11:11:15 +08:00
|
|
|
// 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);
|
|
|
|
|
|
2024-05-27 17:25:20 +08:00
|
|
|
callback&&callback(Arraybuffer)
|
2024-01-05 11:11:15 +08:00
|
|
|
}else{
|
2024-05-27 17:25:20 +08:00
|
|
|
callback&&callback('')
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
// callback(value.message)
|
|
|
|
|
});
|
|
|
|
|
// this.udp.on('message', value => {
|
|
|
|
|
// // 收到的是ArrayBuffer 需要进行转换解析
|
|
|
|
|
// if (value) {
|
|
|
|
|
// let dataView = new DataView(value.message)
|
|
|
|
|
// // console.log(`${TAG} udp message length:${dataView?.byteLength}`);
|
|
|
|
|
// let str = ""
|
|
|
|
|
// for (let i = 0;i < dataView?.byteLength; ++i) {
|
|
|
|
|
// let c = String.fromCharCode(dataView?.getUint8(i))
|
|
|
|
|
// if (c !== "\n") {
|
|
|
|
|
// str += c
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// console.log(`${TAG} udp on message array buffer:${str}`);
|
|
|
|
|
// callback(str)
|
|
|
|
|
// }else{
|
|
|
|
|
// callback('')
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
offTcp(callback) {
|
|
|
|
|
this.tcp.off('message', callback);
|
|
|
|
|
}
|
|
|
|
|
closeTcp(callback) {
|
|
|
|
|
return new Promise((reslove,reject)=>{
|
|
|
|
|
let promise = this.tcp.close();
|
|
|
|
|
promise.then(() => {
|
|
|
|
|
console.log('close success');
|
2024-05-16 09:53:10 +08:00
|
|
|
callback()
|
2024-01-05 11:11:15 +08:00
|
|
|
reslove(true)
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.log('close fail');
|
|
|
|
|
reslove(false)
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|