Compare commits
No commits in common. "dcbcf9d1adbc95b2567094e4e61f68b3d70cdd37" and "eba191b4e0362cb7eb87ea7381bf3e0fd51966a3" have entirely different histories.
dcbcf9d1ad
...
eba191b4e0
@ -10,7 +10,6 @@ export async function sendMsg(val) {
|
|||||||
// globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
|
// globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// obj
|
|
||||||
export async function getUDP(context, errorFlag?) {
|
export async function getUDP(context, errorFlag?) {
|
||||||
return new Promise(async (reslove, reject) => {
|
return new Promise(async (reslove, reject) => {
|
||||||
const fileUtil = new FileUtil(context)
|
const fileUtil = new FileUtil(context)
|
||||||
@ -77,7 +76,6 @@ export async function getUDP(context, errorFlag?) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 中心
|
|
||||||
export async function getUDP2(context, errorFlag?) {
|
export async function getUDP2(context, errorFlag?) {
|
||||||
const fileUtil = new FileUtil(context)
|
const fileUtil = new FileUtil(context)
|
||||||
const carInfo=AppStorage.get('carInfo')
|
const carInfo=AppStorage.get('carInfo')
|
||||||
@ -161,7 +159,6 @@ export async function getUDP2(context, errorFlag?) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 灯光
|
|
||||||
export async function setTopLineUdp() {
|
export async function setTopLineUdp() {
|
||||||
const context=AppStorage.get('context')
|
const context=AppStorage.get('context')
|
||||||
const fileUtil = new FileUtil(context)
|
const fileUtil = new FileUtil(context)
|
||||||
@ -184,7 +181,6 @@ export async function setTopLineUdp() {
|
|||||||
|
|
||||||
//
|
//
|
||||||
let judgeUdpTimer
|
let judgeUdpTimer
|
||||||
// 评判
|
|
||||||
export async function setJudgeUdp() {
|
export async function setJudgeUdp() {
|
||||||
const context=AppStorage.get('context')
|
const context=AppStorage.get('context')
|
||||||
const fileUtil = new FileUtil(context)
|
const fileUtil = new FileUtil(context)
|
||||||
|
|||||||
@ -1,81 +0,0 @@
|
|||||||
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