Compare commits
	
		
			3 Commits
		
	
	
		
			eba191b4e0
			...
			dcbcf9d1ad
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| dcbcf9d1ad | |||
| 78755f3eae | |||
| bb893dcc6e | 
| @ -10,6 +10,7 @@ export async function sendMsg(val) { | ||||
|   // globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
 | ||||
| } | ||||
| 
 | ||||
| // obj
 | ||||
| export async function getUDP(context, errorFlag?) { | ||||
|   return new Promise(async (reslove, reject) => { | ||||
|     const fileUtil = new FileUtil(context) | ||||
| @ -76,6 +77,7 @@ export async function getUDP(context, errorFlag?) { | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| // 中心
 | ||||
| export async function getUDP2(context, errorFlag?) { | ||||
|   const fileUtil = new FileUtil(context) | ||||
|   const carInfo=AppStorage.get('carInfo') | ||||
| @ -159,6 +161,7 @@ export async function getUDP2(context, errorFlag?) { | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| // 灯光
 | ||||
| export async function setTopLineUdp() { | ||||
|   const context=AppStorage.get('context') | ||||
|   const fileUtil = new FileUtil(context) | ||||
| @ -181,6 +184,7 @@ export async function setTopLineUdp() { | ||||
| 
 | ||||
| //
 | ||||
| let judgeUdpTimer | ||||
| // 评判
 | ||||
| export async function setJudgeUdp() { | ||||
|   const context=AppStorage.get('context') | ||||
|   const fileUtil = new FileUtil(context) | ||||
|  | ||||
							
								
								
									
										81
									
								
								entry/src/main/ets/utils/UdpUtils.ets
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								entry/src/main/ets/utils/UdpUtils.ets
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,81 @@ | ||||
| import socket from '@ohos.net.socket' | ||||
| 
 | ||||
| type DealMethod = (value: ArrayBuffer) => string | ||||
| 
 | ||||
| class UdpClient { | ||||
|   private localIp: string = '' | ||||
|   private localIpPort: string = '' | ||||
|   private oppositeIp: string = '' | ||||
|   private oppositeIpPort: string = '' | ||||
|   private messageEvents: Array<Function> = [] | ||||
|   private udp: socket.UDPSocket = null | ||||
|   private disconnectEvents: Array<Function> = [] | ||||
|   private dealMethod: DealMethod | ||||
| 
 | ||||
|   private dealMessage() { | ||||
|     this.udp?.on("message", value => { | ||||
|       let result = this?.dealMethod(value.message) | ||||
|       this.messageEvents.forEach(cb => { | ||||
|         cb(result) | ||||
|       }) | ||||
|     }) | ||||
|   } | ||||
| 
 | ||||
|   init(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) { | ||||
|     this.localIp = udpLocalIp | ||||
|     this.oppositeIp = udpOppositeIp | ||||
|     this.localIpPort = udpLocalIpPort | ||||
|     this.oppositeIpPort = udpOppositeIpPort | ||||
|     this.udp = socket.constructUDPSocketInstance(); | ||||
|   } | ||||
| 
 | ||||
|   bindUdp(): Promise<void> { | ||||
|     return this.udp.bind({ address: this.localIp, port: parseInt(this.localIpPort), family: 1 }).then(() => { | ||||
|       try { | ||||
|         this.dealMessage() | ||||
|         return Promise.resolve() | ||||
|       } catch (e) { | ||||
|         return Promise.reject(e) | ||||
|       } | ||||
|     }) | ||||
|   } | ||||
| 
 | ||||
|   async reBind() { | ||||
|     await this.close() | ||||
|     this.udp = socket.constructUDPSocketInstance(); | ||||
|     await this.bindUdp() | ||||
|   } | ||||
| 
 | ||||
|   close(): Promise<void> { | ||||
|     return this.udp?.close() | ||||
|   } | ||||
| 
 | ||||
|   setDealMethod(fun: DealMethod) { | ||||
|     this.dealMethod = fun | ||||
|   } | ||||
| 
 | ||||
|   onMessage(callback: Function) { | ||||
|     this.messageEvents.push(callback) | ||||
|   } | ||||
| 
 | ||||
|   onDisconnect(callback: Function) { | ||||
|     this.disconnectEvents.push(callback) | ||||
|   } | ||||
| 
 | ||||
|   sendMsg(data: string): Promise<void> { | ||||
|     return this.udp?.getState().then(() => { | ||||
|       return this.udp.send({ | ||||
|         data, | ||||
|         address: { address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1 } | ||||
|       }) | ||||
|     }) | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| export const objUDPClient = new UdpClient() | ||||
| 
 | ||||
| export const centerUDPClient = new UdpClient() | ||||
| 
 | ||||
| export const lightUDPClient = new UdpClient() | ||||
| 
 | ||||
| export const judgeUDPClient = new UdpClient() | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user