tcp修改
This commit is contained in:
		
							parent
							
								
									33a719a462
								
							
						
					
					
						commit
						3bbf35a1db
					
				| @ -1,12 +1,13 @@ | ||||
| import TcpClient from './TcpClient'; | ||||
| import { getSyncData} from '../service/initable' | ||||
| import { getSyncData } from '../service/initable'; | ||||
| import hilog from '@ohos.hilog'; | ||||
| 
 | ||||
| export async function getTCP() { | ||||
| 
 | ||||
|   getSyncData('IpConfigTable').then(async (result: Array<any>) => { | ||||
|     console.log('result222', JSON.stringify(result)) | ||||
|     if (result.length) { | ||||
|       if(globalThis.TcpClient&&globalThis.getCloseTcp){ | ||||
|       if (globalThis.TcpClient && globalThis.TcpClient.closeTcp) { | ||||
|         globalThis.TcpClient.closeTcp(async () => { | ||||
|           // const tcpClient: TcpClient =new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort)
 | ||||
|           // globalThis.TcpClient = tcpClient
 | ||||
| @ -68,7 +69,8 @@ export async function getTCP() { | ||||
| 
 | ||||
|     } else { | ||||
|       globalThis.TcpClient = {} | ||||
|       globalThis.TcpClient.onMessage=()=>{} | ||||
|       globalThis.TcpClient.onMessage = () => { | ||||
|       } | ||||
|     } | ||||
|   }) | ||||
| } | ||||
|  | ||||
| @ -1,9 +1,7 @@ | ||||
| import socket from '@ohos.net.socket'; | ||||
| import buffer from '@ohos.buffer'; | ||||
| import hilog from '@ohos.hilog'; | ||||
| 
 | ||||
| const TAG = 'socketTag[TcpDemo.TcpClient]' | ||||
| import prompt from '@ohos.prompt' | ||||
| import hilog from '@ohos.hilog'; | ||||
| 
 | ||||
| export default class TcpClient { | ||||
|   private localIp: string = '' | ||||
| @ -21,6 +19,7 @@ export default class TcpClient { | ||||
|     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)) | ||||
| @ -34,6 +33,7 @@ export default class TcpClient { | ||||
|       // })
 | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   rebindTcp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) { | ||||
|     return new Promise((resolve, reject) => { | ||||
|       this.localIp = localIp | ||||
| @ -41,7 +41,9 @@ export default class TcpClient { | ||||
|       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 => { | ||||
|       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)); | ||||
| @ -61,10 +63,13 @@ export default class TcpClient { | ||||
|     // });
 | ||||
| 
 | ||||
|   } | ||||
| 
 | ||||
|   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 => { | ||||
|       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 | ||||
| @ -87,10 +92,15 @@ export default class TcpClient { | ||||
| 
 | ||||
| 
 | ||||
|   } | ||||
| 
 | ||||
|   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}); | ||||
|       let promise = this.tcp.connect({ | ||||
|         address: { | ||||
|           address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1 | ||||
|         }, timeout: 6000 | ||||
|       }); | ||||
|       promise.then(() => { | ||||
| 
 | ||||
|         this.tcp.setExtraOptions({ | ||||
| @ -113,6 +123,7 @@ export default class TcpClient { | ||||
|         globalThis.getCloseTcp = true | ||||
|         setTimeout(() => { | ||||
|           this.connectTcp() | ||||
|           resolve(false) | ||||
|         }, 2000) | ||||
|         console.log('testTagconnect,error') | ||||
| 
 | ||||
| @ -153,11 +164,13 @@ export default class TcpClient { | ||||
|       // 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') | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user