From b52fc59329395b8fa62fbfef3bb4fd4375d4a9f6 Mon Sep 17 00:00:00 2001 From: acgist <289547414@qq.com> Date: Sat, 4 May 2024 22:22:10 +0800 Subject: [PATCH] =?UTF-8?q?[*]=20=E8=BF=9E=E6=8E=A5=E4=BF=A1=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../taoyao/media/oh-package.json5 | 4 +- .../taoyao/media/src/main/cpp/bind.cpp | 4 +- .../src/main/cpp/types/libtaoyao/index.d.ts | 26 +- .../main/cpp/types/libtaoyao/oh-package.json5 | 2 +- .../taoyao/media/src/main/ets/pages/Index.ets | 4 +- .../src/main/ets/taoyao/TaoyaoSignal.ets | 415 ++++++++++-------- 6 files changed, 247 insertions(+), 208 deletions(-) diff --git a/taoyao-client-openharmony/taoyao/media/oh-package.json5 b/taoyao-client-openharmony/taoyao/media/oh-package.json5 index ebdda39..217c8aa 100644 --- a/taoyao-client-openharmony/taoyao/media/oh-package.json5 +++ b/taoyao-client-openharmony/taoyao/media/oh-package.json5 @@ -5,7 +5,9 @@ "version": "1.0.0", "license": "Apache-2.0", "description": "桃夭媒体终端", - "dependencies" : {}, + "dependencies" : { + "libtaoyao.so": "file:./src/main/cpp/types/libtaoyao" + }, "devDependencies" : {}, "dynamicDependencies": {} } diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/bind.cpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/bind.cpp index 25a9bc7..ea211bd 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/bind.cpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/bind.cpp @@ -70,7 +70,7 @@ static napi_value Init(napi_env env, napi_value exports) { } EXTERN_C_END -static napi_module taoyaoModule = { +static napi_module libtaoyaoModule = { .nm_version = 1, .nm_flags = 0, .nm_filename = nullptr, @@ -81,6 +81,6 @@ static napi_module taoyaoModule = { }; extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { - napi_module_register(&taoyaoModule); + napi_module_register(&libtaoyaoModule); acgist::init(); } diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/index.d.ts b/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/index.d.ts index 22fdab1..5a393cd 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/index.d.ts +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/index.d.ts @@ -1,13 +1,13 @@ -export const mediaConsume : () => void; -export const mediaConsumerClose : () => void; -export const mediaConsumerPause : () => void; -export const mediaConsumerResume: () => void; -export const mediaProducerClose : () => void; -export const mediaProducerPause : () => void; -export const mediaProducerResume: () => void; -export const roomClientList : () => void; -export const roomClose : () => void; -export const roomEnter : () => void; -export const roomExpel : () => void; -export const roomInvite: () => void; -export const roomLeave : () => void; +export const mediaConsume : () => number; +export const mediaConsumerClose : () => number; +export const mediaConsumerPause : () => number; +export const mediaConsumerResume: () => number; +export const mediaProducerClose : () => number; +export const mediaProducerPause : () => number; +export const mediaProducerResume: () => number; +export const roomClientList : () => number; +export const roomClose : () => number; +export const roomEnter : () => number; +export const roomExpel : () => number; +export const roomInvite: () => number; +export const roomLeave : () => number; diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/oh-package.json5 b/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/oh-package.json5 index e58a0b7..c74a3cd 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/oh-package.json5 +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/oh-package.json5 @@ -1,6 +1,6 @@ { "name": "libtaoyao.so", "types": "./index.d.ts", - "version": "", + "version": "1.0.0", "description": "桃夭媒体依赖" } \ No newline at end of file diff --git a/taoyao-client-openharmony/taoyao/media/src/main/ets/pages/Index.ets b/taoyao-client-openharmony/taoyao/media/src/main/ets/pages/Index.ets index 6d03b7e..86b2498 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/ets/pages/Index.ets +++ b/taoyao-client-openharmony/taoyao/media/src/main/ets/pages/Index.ets @@ -11,7 +11,7 @@ struct Index { .fontSize(20) .fontWeight(FontWeight.Bold) .onClick(() => { - // signal.connect(); + signal.connect(); }); } .width('50%'); @@ -20,7 +20,7 @@ struct Index { .fontSize(20) .fontWeight(FontWeight.Bold) .onClick(() => { - // signal.close(); + signal.close(); }); } .width('50%'); diff --git a/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/TaoyaoSignal.ets b/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/TaoyaoSignal.ets index 5bcb1a1..ec8f575 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/TaoyaoSignal.ets +++ b/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/TaoyaoSignal.ets @@ -4,203 +4,240 @@ * @author acgist */ -import hilog from '@ohos.hilog'; -import webSocket from '@ohos.net.webSocket'; +import hilog from "@ohos.hilog"; +import webSocket from "@ohos.net.webSocket"; -import { setting } from './Setting'; -import libtaoyao from 'libtaoyao.so' +import { setting } from "./Setting"; + +import taoyaoModule from "libtaoyao.so"; interface BusinessError extends Error { - code: number; + code : number; data?: T; } class TaoyaoSignal { - // // WebSocket信令连接 - // socket: webSocket.WebSocket; - // // 是否关闭 - // closed: boolean = false; - // // 是否连接成功 - // connected: boolean = false; - // // 心跳定时 - // heartbeatTimer: number; - // // 同步请求 - // callbackMapping = new Map(); - // // 当前消息索引 - // index: number = 0; - // // 最大消息索引 - // maxIndex: number = 999; - // // 当前终端索引 - // clientIndex: number = 99999; - // - // connect() { - // if(this.socket) { - // this.socket.close(); - // } - // this.connected = false; - // hilog.info(0x0000, 'TaoyaoSignal', '连接信令:%s', setting.signalAddress); - // this.socket = webSocket.createWebSocket(); - // this.socket.on('open', (err: BusinessError, value: Object) => { - // hilog.info(0x0000, 'TaoyaoSignal', '打开信令:%s', setting.signalAddress); - // this.register(); - // this.connected = true; - // }); - // this.socket.on('message', (err: BusinessError, value: string | ArrayBuffer) => { - // hilog.debug(0x0000, 'TaoyaoSignal', '信令消息:%s', value); - // try { - // this.onMessage(value); - // } catch (error) { - // hilog.error(0x0000, 'TaoyaoSignal', '处理信令消息异常:%s', value, error); - // } - // }); - // this.socket.on('close', (err: BusinessError, value: Object) => { - // hilog.error(0x0000, 'TaoyaoSignal', '关闭信令:%s', setting.signalAddress, err); - // this.reconnect(); - // }); - // this.socket.on('error', (err: BusinessError) => { - // hilog.error(0x0000, 'TaoyaoSignal', '信令异常:%s', setting.signalAddress, err); - // this.reconnect(); - // }); - // this.socket.connect(setting.signalAddress, (err: BusinessError, value: boolean) => { - // hilog.info(0x0000, 'TaoyaoSignal', '信令连接成功:%s', setting.signalAddress); - // }); - // }; - // - // reconnect() { - // if(!this.closed) { - // setTimeout(() => this.connect(), 5000); - // } - // } - // - // close() { - // hilog.info(0x0000, 'TaoyaoSignal', '关闭信令:%s', setting.signalAddress); - // this.closed = true; - // this.connected = false; - // if(this.socket) { - // this.socket.close(); - // } - // } - // - // async register() { - // const response: any = await this.request("client::register", { - // name : setting.signal.name, - // clientId : setting.signal.clientId, - // clientType: setting.signal.clientType, - // username : setting.signal.username, - // password : setting.signal.password, - // battery : 100, - // charging : true, - // }); - // const { body } = response; - // const { index } = body; - // this.clientIndex = index; - // hilog.info(0x0000, "TaoyaoSignal", "信令注册成功:%d", index); - // this.heartbeat(); - // } - // - // heartbeat() { - // if(this.heartbeatTimer) { - // clearInterval(this.heartbeatTimer); - // } - // this.heartbeatTimer = setInterval(() => { - // this.send("client::heartbeat", { - // battery : 100, - // charging: true, - // }); - // }, 30 * 1000); - // }; - // - // buildId() { - // if (++this.index > this.maxIndex) { - // this.index = 0; - // } - // const date = new Date(); - // return ( - // 100000000000000 * date.getDate() + - // 1000000000000 * date.getHours() + - // 10000000000 * date.getMinutes() + - // 100000000 * date.getSeconds() + - // 1000 * this.clientIndex + - // this.index - // ); - // } - // - // send(signal, body) { - // const header = { - // v: setting.version, - // id: this.buildId(), - // signal - // }; - // const message = { - // header, - // body - // }; - // try { - // this.socket.send(JSON.stringify(message)); - // } catch (error) { - // hilog.error(0x0000, 'TaoyaoSignal', '发送消息异常:%o', message, error); - // } - // } - // - // async request(signal, body) { - // const id = this.buildId(); - // return new Promise((resolve, reject) => { - // const header = { - // v: setting.version, - // id, - // signal - // }; - // const message = { - // header, - // body - // }; - // // 设置超时 - // const rejectTimeout = setTimeout(() => { - // this.callbackMapping.delete(id); - // reject("请求超时"); - // }, 5000); - // // 请求回调 - // this.callbackMapping.set(id, (response) => { - // resolve(response); - // clearTimeout(rejectTimeout); - // // 默认不用继续处理 - // return true; - // }); - // // 发送消息 - // try { - // this.socket.send(JSON.stringify(message)); - // } catch (error) { - // hilog.error(0x0000, 'TaoyaoSignal', '发送消息异常:%o', message, error); - // reject(error); - // } - // }); - // } - // - // onMessage(message) { - // const json = JSON.parse(message); - // const { - // header, - // body, - // } = json; - // const { - // id - // } = header; - // if (this.callbackMapping.has(id)) { - // hilog.info(0x0000, "TaoyaoSignal", "处理同步消息:%s", message); - // try { - // if( - // this.callbackMapping.get(id)(json) - // ) { - // return; - // } - // } finally { - // this.callbackMapping.delete(id); - // } - // } - // hilog.info(0x0000, "TaoyaoSignal", "处理异步消息:%s", message); - // // TODO - // } + // WebSocket信令连接 + socket : webSocket.WebSocket | null = null; + // 是否关闭 + closed : boolean = false; + // 是否连接成功 + connected: boolean = false; + // 心跳定时 + heartbeatTimer: number = 0; + // 同步请求 + callbackMapping = new Map(); + // 当前消息索引 + index : number = 0; + // 最大消息索引 + maxIndex : number = 999; + // 当前终端索引 + clientIndex: number = 99999; + + /** + * 连接信令 + */ + connect() { + if(this.socket) { + hilog.info(0x0000, "TaoyaoSignal", "信令以及连接关闭旧的连接:%s", setting.signalAddress); + this.socket.close(); + } + this.connected = false; + hilog.info(0x0000, "TaoyaoSignal", "连接信令:%s", setting.signalAddress); + this.socket = webSocket.createWebSocket(); + this.socket.on("open", (err: BusinessError, value: Object) => { + hilog.info(0x0000, "TaoyaoSignal", "打开信令:%s", setting.signalAddress); + this.register(); + this.connected = true; + }); + this.socket.on("message", (err: BusinessError, value: string | ArrayBuffer) => { + hilog.debug(0x0000, "TaoyaoSignal", "信令消息:%s", value); + try { + this.onMessage(value.toString()); + } catch (error) { + hilog.error(0x0000, "TaoyaoSignal", "处理信令消息异常:%s", value, error); + } + }); + this.socket.on("close", (err: BusinessError, value: Object) => { + hilog.error(0x0000, "TaoyaoSignal", "关闭信令:%s", setting.signalAddress, err); + this.reconnect(); + }); + this.socket.on("error", (err: BusinessError) => { + hilog.error(0x0000, "TaoyaoSignal", "信令异常:%s", setting.signalAddress, err); + this.reconnect(); + }); + this.socket.connect(setting.signalAddress, (err: BusinessError, value: boolean) => { + hilog.info(0x0000, "TaoyaoSignal", "信令连接成功:%s", setting.signalAddress); + }); + }; + + /** + * 重连信令 + */ + reconnect() { + if(this.closed) { + // 已经关闭忽略重连 + } else { + hilog.info(0x0000, "TaoyaoSignal", "重连信令连接:%s", setting.signalAddress); + setTimeout((): void => this.connect(), 5000); + } + } + + /** + * 关闭信令 + */ + close() { + hilog.info(0x0000, "TaoyaoSignal", "关闭信令:%s", setting.signalAddress); + this.closed = true; + this.connected = false; + if(this.socket) { + this.socket.close(); + } + } + + /** + * 注册信令 + */ + async register() { + const response: Record = await this.request("client::register", { + "name" : setting.signal.name, + "clientId" : setting.signal.clientId, + "clientType": setting.signal.clientType, + "username" : setting.signal.username, + "password" : setting.signal.password, + "battery" : 100, + "charging" : true + }); + const body : Record = response.body as Record; + const index : number = body.index as number; + this.clientIndex = index; + hilog.info(0x0000, "TaoyaoSignal", "信令注册成功:%d", index); + this.heartbeat(); + } + + /** + * 心跳 + */ + heartbeat() { + if(this.heartbeatTimer) { + clearInterval(this.heartbeatTimer); + } + this.heartbeatTimer = setInterval(() => { + this.send("client::heartbeat", { + "battery" : 100, + "charging": true, + }); + }, 30 * 1000); + }; + + /** + * @returns ID + */ + buildId(): number { + if (++this.index > this.maxIndex) { + this.index = 0; + } + const date = new Date(); + return ( + 100000000000000 * date.getDate() + + 1000000000000 * date.getHours() + + 10000000000 * date.getMinutes() + + 100000000 * date.getSeconds() + + 1000 * this.clientIndex + + this.index + ); + } + + /** + * 发送消息 + * + * @param signal 信令 + * @param body 主体 + */ + send(signal: string, body: Record) { + const header: Record = { + "v" : setting.version, + "id" : this.buildId(), + "signal": signal + }; + const message: Record = { + "header": header, + "body" : body + }; + try { + this.socket?.send(JSON.stringify(message)); + } catch (error) { + hilog.error(0x0000, "TaoyaoSignal", "发送消息异常:%o", message, error); + } + } + + /** + * 请求消息 + * + * @param signal 信令 + * @param body 主体 + * + * @returns 响应 + */ + async request(signal: string, body: Record): Promise> { + const id = this.buildId(); + return new Promise>((resolve, reject) => { + const header: Record = { + "v" : setting.version, + "id" : id, + "signal": signal + }; + const message: Record = { + "header": header, + "body" : body + }; + // 设置超时 + const rejectTimeout = setTimeout(() => { + this.callbackMapping.delete(id); + reject("请求超时"); + }, 5000); + // 请求回调 + this.callbackMapping.set(id, (response: Record) => { + resolve(response); + clearTimeout(rejectTimeout); + // 默认不用继续处理 + return true; + }); + // 发送消息 + try { + this.socket?.send(JSON.stringify(message)); + } catch (error) { + hilog.error(0x0000, "TaoyaoSignal", "发送消息异常:%o", message, error); + reject(error); + } + }); + } + + /** + * 处理消息 + * + * @param message 消息 + */ + onMessage(message: string) { + const json : Record = JSON.parse(message); + const header: Record = json.header as Record; + const body : Record = json.body as Record; + const id : number = header.id as number; + if (this.callbackMapping.has(id)) { + hilog.debug(0x0000, "TaoyaoSignal", "处理同步消息:%s", message); + try { + const callback: Function = this.callbackMapping.get(id) as Function; + if(callback(json)) { + return; + } + } finally { + this.callbackMapping.delete(id); + } + } + hilog.debug(0x0000, "TaoyaoSignal", "处理异步消息:%s", message); + // TODO + } }