GlobalUdp.ts 优化 #13
@ -1,13 +1,136 @@
|
|||||||
import UdpClient from './UdpClient';
|
import UdpClient from './UdpClient';
|
||||||
import UdpClientByCenter from './UdpClientByCenter';
|
import UdpClientByCenter from './UdpClientByCenter';
|
||||||
import { getSyncData } from '../service/initable'
|
import { getSyncData } from '../service/initable'
|
||||||
import { dateFormat } from '../../common/utils/tools'
|
import { fillZero, string2Bytes } from '../utils/tools'
|
||||||
import prompt from '@ohos.prompt'
|
|
||||||
import TcpClient from './TcpClient';
|
interface UdpOption {
|
||||||
import {string2Bytes} from '../../common/utils/tools'
|
udplocalIp: string
|
||||||
export async function sendMsg(val){
|
udplocalIpPort: string
|
||||||
// globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
|
udpOppositeIp: string
|
||||||
|
udpOppositeIpPort: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default class UdpUtils {
|
||||||
|
private static config: UdpOption
|
||||||
|
|
||||||
|
public static async getConfig() {
|
||||||
|
this.config = ((await getSyncData("IpConfigTable")) as Array<any> || [])[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async createUdpClient(client: UdpClient | UdpClientByCenter, udpConfig: UdpOption, type: string, onMessage?: Function) {
|
||||||
|
if (!this.config) {
|
||||||
|
return [{
|
||||||
|
onMessage: () => {
|
||||||
|
}
|
||||||
|
}, ""]
|
||||||
|
}
|
||||||
|
if (client) {
|
||||||
|
await client.closeUdp()
|
||||||
|
client.rebindUdp(udpConfig.udplocalIp, udpConfig.udplocalIpPort, udpConfig.udpOppositeIp, udpConfig.udpOppositeIpPort)
|
||||||
|
} else {
|
||||||
|
if (type === "1") {
|
||||||
|
client = new UdpClient(udpConfig.udplocalIp, udpConfig.udplocalIpPort, udpConfig.udpOppositeIp, udpConfig.udpOppositeIpPort)
|
||||||
|
} else {
|
||||||
|
client = new UdpClientByCenter(udpConfig.udplocalIp, udpConfig.udplocalIpPort, udpConfig.udpOppositeIp, udpConfig.udpOppositeIpPort)
|
||||||
|
}
|
||||||
|
client.bindUdp()
|
||||||
|
}
|
||||||
|
if (type === "1") {
|
||||||
|
client.sendMsg("111")
|
||||||
|
}
|
||||||
|
client.onMessage(onMessage || (() => {
|
||||||
|
}))
|
||||||
|
client.onError(client.onMessage || (() => {
|
||||||
|
}))
|
||||||
|
return [client, `http://${this.config[0].centerIp}:${this.config[0].centerPort}`]
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getUdp(client: UdpClient) {
|
||||||
|
return this.createUdpClient(client, {
|
||||||
|
udplocalIp: this.config.udplocalIp,
|
||||||
|
udplocalIpPort: this.config.udplocalIpPort,
|
||||||
|
udpOppositeIp: this.config.udpOppositeIp,
|
||||||
|
udpOppositeIpPort: this.config.udpOppositeIpPort
|
||||||
|
}, "1")
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getUdp2(client: UdpClient) {
|
||||||
|
return this.createUdpClient(client, {
|
||||||
|
udplocalIp: this.config.udplocalIp,
|
||||||
|
udplocalIpPort: "8800",
|
||||||
|
udpOppositeIp: globalThis.carInfo?.udpAddress,
|
||||||
|
udpOppositeIpPort: globalThis.carInfo?.messagePort
|
||||||
|
}, "2", (val) => {
|
||||||
|
if (val.id == '32') {
|
||||||
|
globalThis.signNum = val.body[1]
|
||||||
|
|
||||||
|
} else if (val.id == '46') {
|
||||||
|
let tmpList = []
|
||||||
|
const str = globalThis.lsh
|
||||||
|
for (let i = 0; i < str.length; i++) {
|
||||||
|
tmpList.push(string2Bytes(str.charCodeAt(i), 1 * 8)[0])
|
||||||
|
}
|
||||||
|
const param = {
|
||||||
|
id: 47,
|
||||||
|
list: tmpList,
|
||||||
|
carNo: globalThis.carInfo.carNo,
|
||||||
|
placeId: globalThis.carInfo.examinationRoomId
|
||||||
|
}
|
||||||
|
globalThis.udpClient2.send(param)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async setTopLineUdp() {
|
||||||
|
if (this.config && this.config.udplocalIp) {
|
||||||
|
const {udplocalIp,udpOppositeIp,udpOppositeIpPort} = this.config;
|
||||||
|
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '55509', udpOppositeIp, udpOppositeIpPort)
|
||||||
|
udpClient.bindUdp()
|
||||||
|
return {
|
||||||
|
send(bytes) {
|
||||||
|
udpClient.sendHeadMsg(bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async setJudgeUdp() {
|
||||||
|
let udpIndex = 0;
|
||||||
|
let currentUdpIndex = 0;
|
||||||
|
let judgeUdpTimer
|
||||||
|
clearInterval(judgeUdpTimer)
|
||||||
|
judgeUdpTimer = setInterval(() => {
|
||||||
|
udpIndex += 1;
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
if (this.config && this.config.udplocalIp) {
|
||||||
|
const {udplocalIp} = this.config;
|
||||||
|
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8080', globalThis.carInfo?.gpsAddress, globalThis.carInfo?.hintPort)
|
||||||
|
udpClient.bindUdp()
|
||||||
|
udpClient.onMessage((val) => {
|
||||||
|
console.log('valval', val)
|
||||||
|
})
|
||||||
|
globalThis.judgeUdpClient = udpClient;
|
||||||
|
return {
|
||||||
|
send(bytes) {
|
||||||
|
if (udpIndex > currentUdpIndex) {
|
||||||
|
udpClient.sendMsg({
|
||||||
|
id: 45,
|
||||||
|
list: bytes,
|
||||||
|
carNo: globalThis.carInfo.carNo,
|
||||||
|
placeId: globalThis.carInfo.examinationRoomId
|
||||||
|
})
|
||||||
|
currentUdpIndex = udpIndex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function sendMsg(val) {
|
||||||
|
}
|
||||||
|
|
||||||
export async function getUDP(flag: Boolean = false) {
|
export async function getUDP(flag: Boolean = false) {
|
||||||
getSyncData('IpConfigTable').then((result: Array<any>) => {
|
getSyncData('IpConfigTable').then((result: Array<any>) => {
|
||||||
if (result.length) {
|
if (result.length) {
|
||||||
@ -19,7 +142,8 @@ export async function getUDP(flag:Boolean=false) {
|
|||||||
globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp, result[0].udpOppositeIpPort)
|
globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp, result[0].udpOppositeIpPort)
|
||||||
globalThis.udpClient.sendMsg('111')
|
globalThis.udpClient.sendMsg('111')
|
||||||
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
|
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
|
||||||
globalThis.udpClient.onError(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{})
|
globalThis.udpClient.onError(globalThis.udpClient.onMessage ? globalThis.udpClient.onMessage : () => {
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@ -29,13 +153,15 @@ export async function getUDP(flag:Boolean=false) {
|
|||||||
udpClient.sendMsg('111')
|
udpClient.sendMsg('111')
|
||||||
globalThis.udpClient = udpClient
|
globalThis.udpClient = udpClient
|
||||||
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
|
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
|
||||||
globalThis.udpClient.onError(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{})
|
globalThis.udpClient.onError(globalThis.udpClient.onMessage ? globalThis.udpClient.onMessage : () => {
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
globalThis.udpClient = {}
|
globalThis.udpClient = {}
|
||||||
globalThis.udpClient.onMessage=()=>{}
|
globalThis.udpClient.onMessage = () => {
|
||||||
|
}
|
||||||
globalThis.host = ''
|
globalThis.host = ''
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -53,7 +179,8 @@ export async function getUDP2(flag:Boolean=false) {
|
|||||||
globalThis.udpClient2.onMessage((val) => {
|
globalThis.udpClient2.onMessage((val) => {
|
||||||
})
|
})
|
||||||
globalThis.udpClient2.onError()
|
globalThis.udpClient2.onError()
|
||||||
globalThis.udpClient2.setMsgCallBack=()=>{}
|
globalThis.udpClient2.setMsgCallBack = () => {
|
||||||
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@ -71,15 +198,22 @@ export async function getUDP2(flag:Boolean=false) {
|
|||||||
for (let i = 0; i < str.length; i++) {
|
for (let i = 0; i < str.length; i++) {
|
||||||
tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0])
|
tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0])
|
||||||
}
|
}
|
||||||
const param = { id: 47, list: tmpList, carNo: globalThis.carInfo.carNo, placeId: globalThis.carInfo.examinationRoomId }
|
const param = {
|
||||||
|
id: 47,
|
||||||
|
list: tmpList,
|
||||||
|
carNo: globalThis.carInfo.carNo,
|
||||||
|
placeId: globalThis.carInfo.examinationRoomId
|
||||||
|
}
|
||||||
globalThis.udpClient2.send(param)
|
globalThis.udpClient2.send(param)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
globalThis.udpClient2 = {}
|
globalThis.udpClient2 = {}
|
||||||
globalThis.udpClient2.onMessage=()=>{}
|
globalThis.udpClient2.onMessage = () => {
|
||||||
globalThis.udpClient2.setMsgCallBack=()=>{}
|
}
|
||||||
|
globalThis.udpClient2.setMsgCallBack = () => {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -122,7 +256,12 @@ export async function setJudgeUdp(){
|
|||||||
return {
|
return {
|
||||||
send(bytes) {
|
send(bytes) {
|
||||||
if (udpIndex > currentUdpIndex) {
|
if (udpIndex > currentUdpIndex) {
|
||||||
udpClient.sendMsg({id:45,list:bytes,carNo:globalThis.carInfo.carNo,placeId:globalThis.carInfo.examinationRoomId})
|
udpClient.sendMsg({
|
||||||
|
id: 45,
|
||||||
|
list: bytes,
|
||||||
|
carNo: globalThis.carInfo.carNo,
|
||||||
|
placeId: globalThis.carInfo.examinationRoomId
|
||||||
|
})
|
||||||
currentUdpIndex = udpIndex
|
currentUdpIndex = udpIndex
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,13 +21,15 @@ const TAG = '[PLC.UdpClient]'
|
|||||||
import prompt from '@ohos.prompt'
|
import prompt from '@ohos.prompt'
|
||||||
|
|
||||||
import hilog from '@ohos.hilog';
|
import hilog from '@ohos.hilog';
|
||||||
|
|
||||||
export default class UdpClient {
|
export default class UdpClient {
|
||||||
private localIp: string = ''
|
private localIp: string = ''
|
||||||
private localIpPort: string = ''
|
private localIpPort: string = ''
|
||||||
private oppositeIp: string = ''
|
private oppositeIp: string = ''
|
||||||
private oppositeIpPort: string = ''
|
private oppositeIpPort: string = ''
|
||||||
|
|
||||||
private udp: any = null
|
private udp: socket.UDPSocket = null
|
||||||
|
|
||||||
// private stashFn:StashFuncotion
|
// private stashFn:StashFuncotion
|
||||||
|
|
||||||
constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
|
constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
|
||||||
@ -75,9 +77,11 @@ export default class UdpClient {
|
|||||||
console.log(`${TAG}udpCLient udp bind failed:${JSON.stringify(err)}`);
|
console.log(`${TAG}udpCLient udp bind failed:${JSON.stringify(err)}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setMsgCallBack(callback) {
|
setMsgCallBack(callback) {
|
||||||
// this.stashFn=callback?callback:()=>{}
|
// this.stashFn=callback?callback:()=>{}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMsg(msg) {
|
sendMsg(msg) {
|
||||||
console.log('sendsnd01')
|
console.log('sendsnd01')
|
||||||
hilog.info(0x0000, 'udpCLient', 'send');
|
hilog.info(0x0000, 'udpCLient', 'send');
|
||||||
@ -101,6 +105,7 @@ export default class UdpClient {
|
|||||||
console.log(`${TAG}udpCLient udp send fail:${JSON.stringify(err)}`);
|
console.log(`${TAG}udpCLient udp send fail:${JSON.stringify(err)}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onError(callback?) {
|
onError(callback?) {
|
||||||
this.udp.on('error', async err => {
|
this.udp.on('error', async err => {
|
||||||
console.log('udpdiushierroring')
|
console.log('udpdiushierroring')
|
||||||
@ -183,41 +188,25 @@ export default class UdpClient {
|
|||||||
}, 3000)
|
}, 3000)
|
||||||
}
|
}
|
||||||
|
|
||||||
closeUdp(callback) {
|
closeUdp() {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
this.udp.close(err => {
|
this.udp.close().then(() => {
|
||||||
hilog.info(0x0000, 'udpCLient', 'close');
|
hilog.info(0x0000, 'udpClient', 'close');
|
||||||
|
return this.udp.getState()
|
||||||
if (err) {
|
}).then((data) => {
|
||||||
hilog.info(0x0000, 'udpCLient', 'closeonerror');
|
if (!data.isClose) {
|
||||||
|
|
||||||
} else {
|
|
||||||
this.udp.getState((err, data) => {
|
|
||||||
if (err) {
|
|
||||||
console.log('getState fail');
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
if (!data.isisClose) {
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
callback()
|
resolve()
|
||||||
}, 1000)
|
}, 1000)
|
||||||
}
|
}
|
||||||
}
|
}).catch(err => {
|
||||||
console.log('getState success:' + JSON.stringify(data));
|
hilog.error(0x0000, 'udpClient', err);
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
// let promise = this.udp.getState({});
|
|
||||||
// promise.then(data => {
|
|
||||||
//
|
|
||||||
// console.log('getState success:' + JSON.stringify(data));
|
|
||||||
// }).catch(err => {
|
|
||||||
// callback()
|
|
||||||
// console.log('getState fail');
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
interface StashFunction {
|
interface StashFunction {
|
||||||
(str: string)
|
(str: string)
|
||||||
}
|
}
|
||||||
@ -19,6 +19,7 @@ import mediaLibrary from '@ohos.multimedia.mediaLibrary'
|
|||||||
import FileUtil from '../../common/utils/File'
|
import FileUtil from '../../common/utils/File'
|
||||||
import { fillZero, string2Bytes } from '../utils/tools'
|
import { fillZero, string2Bytes } from '../utils/tools'
|
||||||
import { GlobalConfig } from '../../config/index'
|
import { GlobalConfig } from '../../config/index'
|
||||||
|
|
||||||
const TAG = '[UdpDemo.UdpClient]'
|
const TAG = '[UdpDemo.UdpClient]'
|
||||||
// import common from '@ohos.app.ability.common';
|
// import common from '@ohos.app.ability.common';
|
||||||
import hilog from '@ohos.hilog';
|
import hilog from '@ohos.hilog';
|
||||||
@ -39,16 +40,20 @@ export default class UdpClientByCenter {
|
|||||||
private lsh: string = null
|
private lsh: string = null
|
||||||
private context
|
private context
|
||||||
private stashFn: StashFunction
|
private stashFn: StashFunction
|
||||||
private headLenth:number=9 //消息头长度
|
private headLenth: number = 9
|
||||||
|
//消息头长度
|
||||||
private initParam
|
private initParam
|
||||||
|
|
||||||
constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
|
constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
|
||||||
this.localIp = udplocalIp
|
this.localIp = udplocalIp
|
||||||
this.oppositeIp = udpOppositeIp
|
this.oppositeIp = udpOppositeIp
|
||||||
this.localIpPort = udplocalIpPort
|
this.localIpPort = udplocalIpPort
|
||||||
this.oppositeIpPort = udpOppositeIpPort
|
this.oppositeIpPort = udpOppositeIpPort
|
||||||
this.stashFn=()=>{}
|
this.stashFn = () => {
|
||||||
|
}
|
||||||
this.udp = socket.constructUDPSocketInstance();
|
this.udp = socket.constructUDPSocketInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
rebindUdp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) {
|
rebindUdp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) {
|
||||||
this.localIp = localIp
|
this.localIp = localIp
|
||||||
this.oppositeIp = oppositeIp
|
this.oppositeIp = oppositeIp
|
||||||
@ -67,10 +72,12 @@ export default class UdpClientByCenter {
|
|||||||
console.log(`${TAG} udp bind failed:${JSON.stringify(err)}`);
|
console.log(`${TAG} udp bind failed:${JSON.stringify(err)}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
initHeartSendMsg(param, context) {
|
initHeartSendMsg(param, context) {
|
||||||
this.initParam = param
|
this.initParam = param
|
||||||
this.context = context
|
this.context = context
|
||||||
}
|
}
|
||||||
|
|
||||||
bindUdp() {
|
bindUdp() {
|
||||||
let promise = this.udp.bind({
|
let promise = this.udp.bind({
|
||||||
address: this.localIp, port: parseInt(this.localIpPort), family: 1
|
address: this.localIp, port: parseInt(this.localIpPort), family: 1
|
||||||
@ -91,15 +98,16 @@ export default class UdpClientByCenter {
|
|||||||
}
|
}
|
||||||
return [result];
|
return [result];
|
||||||
}
|
}
|
||||||
|
|
||||||
Array2Byte(array) {
|
Array2Byte(array) {
|
||||||
var buf = new ArrayBuffer(array.length);
|
var buf = new ArrayBuffer(array.length);
|
||||||
var view = new Uint8Array(buf);
|
var view = new Uint8Array(buf);
|
||||||
for (var i = 0; i != array.length; ++i)
|
for (var i = 0; i != array.length; ++i) {
|
||||||
{
|
|
||||||
view[i] = array[i] & 0xFF;
|
view[i] = array[i] & 0xFF;
|
||||||
}
|
}
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
//length消息体bufferlength id消息类型id bodyStr消息体string
|
//length消息体bufferlength id消息类型id bodyStr消息体string
|
||||||
// setMsyBody(id,bodyByte){
|
// setMsyBody(id,bodyByte){
|
||||||
|
|
||||||
@ -115,6 +123,7 @@ export default class UdpClientByCenter {
|
|||||||
return this.Array2Byte(arr).buffer
|
return this.Array2Byte(arr).buffer
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setMsgHead({id, list, placeId=62, carNo=489}) {
|
setMsgHead({id, list, placeId=62, carNo=489}) {
|
||||||
let a = string2Bytes(`${id}${fillZero(placeId, 3)}`, 2 * 8);
|
let a = string2Bytes(`${id}${fillZero(placeId, 3)}`, 2 * 8);
|
||||||
let b = string2Bytes(`${carNo}${this.lsh}`, 4 * 8);
|
let b = string2Bytes(`${carNo}${this.lsh}`, 4 * 8);
|
||||||
@ -122,6 +131,7 @@ export default class UdpClientByCenter {
|
|||||||
let c = string2Bytes(list.length, 2 * 8);
|
let c = string2Bytes(list.length, 2 * 8);
|
||||||
return [...a, ...b, ...c];
|
return [...a, ...b, ...c];
|
||||||
}
|
}
|
||||||
|
|
||||||
setMsgBody({id,list}) {
|
setMsgBody({id,list}) {
|
||||||
let tmpList = []
|
let tmpList = []
|
||||||
tmpList = list
|
tmpList = list
|
||||||
@ -146,9 +156,12 @@ export default class UdpClientByCenter {
|
|||||||
console.log(`${TAG} udpLine send fail:${JSON.stringify(err)}`);
|
console.log(`${TAG} udpLine send fail:${JSON.stringify(err)}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setMsgCallBack(callback) {
|
setMsgCallBack(callback) {
|
||||||
this.stashFn=callback?callback:()=>{}
|
this.stashFn = callback ? callback : () => {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sendMsg(param, context?) {
|
sendMsg(param, context?) {
|
||||||
console.log('paramparam', JSON.stringify(param))
|
console.log('paramparam', JSON.stringify(param))
|
||||||
if (context) {
|
if (context) {
|
||||||
@ -177,6 +190,7 @@ export default class UdpClientByCenter {
|
|||||||
console.log(`${TAG} udp send fail:${JSON.stringify(err)}`);
|
console.log(`${TAG} udp send fail:${JSON.stringify(err)}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessage(callback, type?) {
|
onMessage(callback, type?) {
|
||||||
this.udp.on('message', value => {
|
this.udp.on('message', value => {
|
||||||
let arr = []
|
let arr = []
|
||||||
@ -194,10 +208,15 @@ export default class UdpClientByCenter {
|
|||||||
for (let i = this.headLenth; i <= this.headLenth + length - 1; i++) {
|
for (let i = this.headLenth; i <= this.headLenth + length - 1; i++) {
|
||||||
list.push(arr[i])
|
list.push(arr[i])
|
||||||
}
|
}
|
||||||
this.stashFn({id,length,body:list,sendId:this.sendId})
|
this.stashFn({
|
||||||
callback({id,length,body:list,sendId:this.sendId})
|
id, length, body: list, sendId: this.sendId
|
||||||
|
})
|
||||||
|
callback({
|
||||||
|
id, length, body: list, sendId: this.sendId
|
||||||
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onError(callback?) {
|
onError(callback?) {
|
||||||
this.udp.on('error', err => {
|
this.udp.on('error', err => {
|
||||||
console.log('udpbycenterdiushierroring')
|
console.log('udpbycenterdiushierroring')
|
||||||
@ -208,35 +227,31 @@ export default class UdpClientByCenter {
|
|||||||
// })
|
// })
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
closeUdp(callback) {
|
|
||||||
this.udp.close(err=>{
|
|
||||||
if(err){
|
|
||||||
|
|
||||||
}else{
|
closeUdp() {
|
||||||
this.udp.getState((err, data) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
if (err) {
|
this.udp.close().then(() => {
|
||||||
return;
|
hilog.info(0x0000, 'udpClient', 'close');
|
||||||
}else{
|
return this.udp.getState()
|
||||||
if(!data.isisClose){
|
}).then((data) => {
|
||||||
|
if (!data.isClose) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
callback()
|
resolve()
|
||||||
}, 1000)
|
}, 1000)
|
||||||
}
|
}
|
||||||
}
|
}).catch(err => {
|
||||||
|
hilog.error(0x0000, 'udpClient', err);
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
// let promise = this.udp.getState({});
|
|
||||||
// promise.then(data => {
|
|
||||||
//
|
|
||||||
// console.log('getState success:' + JSON.stringify(data));
|
|
||||||
// }).catch(err => {
|
|
||||||
// callback()
|
|
||||||
// console.log('getState fail');
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
interface StashFunction {
|
interface StashFunction {
|
||||||
(params: { id: number; length: number; body: any[]; sendId: string }): void;
|
(params: {
|
||||||
|
id: number;
|
||||||
|
length: number;
|
||||||
|
body: any[];
|
||||||
|
sendId: string
|
||||||
|
}): void;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user