diff --git a/entry/src/main/ets/common/utils/GlobalUdp.ts b/entry/src/main/ets/common/utils/GlobalUdp.ts index d0223864..75a8ec18 100644 --- a/entry/src/main/ets/common/utils/GlobalUdp.ts +++ b/entry/src/main/ets/common/utils/GlobalUdp.ts @@ -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) diff --git a/entry/src/main/ets/utils/UdpUtils.ets b/entry/src/main/ets/utils/UdpUtils.ets new file mode 100644 index 00000000..c0279966 --- /dev/null +++ b/entry/src/main/ets/utils/UdpUtils.ets @@ -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 = [] + private udp: socket.UDPSocket = null + private disconnectEvents: Array = [] + 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 { + 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 { + 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 { + 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()