[*] 连接信令
This commit is contained in:
@@ -5,7 +5,9 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"description": "桃夭媒体终端",
|
"description": "桃夭媒体终端",
|
||||||
"dependencies" : {},
|
"dependencies" : {
|
||||||
|
"libtaoyao.so": "file:./src/main/cpp/types/libtaoyao"
|
||||||
|
},
|
||||||
"devDependencies" : {},
|
"devDependencies" : {},
|
||||||
"dynamicDependencies": {}
|
"dynamicDependencies": {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ static napi_value Init(napi_env env, napi_value exports) {
|
|||||||
}
|
}
|
||||||
EXTERN_C_END
|
EXTERN_C_END
|
||||||
|
|
||||||
static napi_module taoyaoModule = {
|
static napi_module libtaoyaoModule = {
|
||||||
.nm_version = 1,
|
.nm_version = 1,
|
||||||
.nm_flags = 0,
|
.nm_flags = 0,
|
||||||
.nm_filename = nullptr,
|
.nm_filename = nullptr,
|
||||||
@@ -81,6 +81,6 @@ static napi_module taoyaoModule = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
extern "C" __attribute__((constructor)) void RegisterEntryModule(void) {
|
extern "C" __attribute__((constructor)) void RegisterEntryModule(void) {
|
||||||
napi_module_register(&taoyaoModule);
|
napi_module_register(&libtaoyaoModule);
|
||||||
acgist::init();
|
acgist::init();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
export const mediaConsume : () => void;
|
export const mediaConsume : () => number;
|
||||||
export const mediaConsumerClose : () => void;
|
export const mediaConsumerClose : () => number;
|
||||||
export const mediaConsumerPause : () => void;
|
export const mediaConsumerPause : () => number;
|
||||||
export const mediaConsumerResume: () => void;
|
export const mediaConsumerResume: () => number;
|
||||||
export const mediaProducerClose : () => void;
|
export const mediaProducerClose : () => number;
|
||||||
export const mediaProducerPause : () => void;
|
export const mediaProducerPause : () => number;
|
||||||
export const mediaProducerResume: () => void;
|
export const mediaProducerResume: () => number;
|
||||||
export const roomClientList : () => void;
|
export const roomClientList : () => number;
|
||||||
export const roomClose : () => void;
|
export const roomClose : () => number;
|
||||||
export const roomEnter : () => void;
|
export const roomEnter : () => number;
|
||||||
export const roomExpel : () => void;
|
export const roomExpel : () => number;
|
||||||
export const roomInvite: () => void;
|
export const roomInvite: () => number;
|
||||||
export const roomLeave : () => void;
|
export const roomLeave : () => number;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libtaoyao.so",
|
"name": "libtaoyao.so",
|
||||||
"types": "./index.d.ts",
|
"types": "./index.d.ts",
|
||||||
"version": "",
|
"version": "1.0.0",
|
||||||
"description": "桃夭媒体依赖"
|
"description": "桃夭媒体依赖"
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,7 @@ struct Index {
|
|||||||
.fontSize(20)
|
.fontSize(20)
|
||||||
.fontWeight(FontWeight.Bold)
|
.fontWeight(FontWeight.Bold)
|
||||||
.onClick(() => {
|
.onClick(() => {
|
||||||
// signal.connect();
|
signal.connect();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
.width('50%');
|
.width('50%');
|
||||||
@@ -20,7 +20,7 @@ struct Index {
|
|||||||
.fontSize(20)
|
.fontSize(20)
|
||||||
.fontWeight(FontWeight.Bold)
|
.fontWeight(FontWeight.Bold)
|
||||||
.onClick(() => {
|
.onClick(() => {
|
||||||
// signal.close();
|
signal.close();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
.width('50%');
|
.width('50%');
|
||||||
|
|||||||
@@ -4,203 +4,240 @@
|
|||||||
* @author acgist
|
* @author acgist
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import hilog from '@ohos.hilog';
|
import hilog from "@ohos.hilog";
|
||||||
import webSocket from '@ohos.net.webSocket';
|
import webSocket from "@ohos.net.webSocket";
|
||||||
|
|
||||||
import { setting } from './Setting';
|
import { setting } from "./Setting";
|
||||||
import libtaoyao from 'libtaoyao.so'
|
|
||||||
|
import taoyaoModule from "libtaoyao.so";
|
||||||
|
|
||||||
interface BusinessError<T = void> extends Error {
|
interface BusinessError<T = void> extends Error {
|
||||||
code: number;
|
code : number;
|
||||||
data?: T;
|
data?: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
class TaoyaoSignal {
|
class TaoyaoSignal {
|
||||||
|
|
||||||
// // WebSocket信令连接
|
// WebSocket信令连接
|
||||||
// socket: webSocket.WebSocket;
|
socket : webSocket.WebSocket | null = null;
|
||||||
// // 是否关闭
|
// 是否关闭
|
||||||
// closed: boolean = false;
|
closed : boolean = false;
|
||||||
// // 是否连接成功
|
// 是否连接成功
|
||||||
// connected: boolean = false;
|
connected: boolean = false;
|
||||||
// // 心跳定时
|
// 心跳定时
|
||||||
// heartbeatTimer: number;
|
heartbeatTimer: number = 0;
|
||||||
// // 同步请求
|
// 同步请求
|
||||||
// callbackMapping = new Map();
|
callbackMapping = new Map<number, Function>();
|
||||||
// // 当前消息索引
|
// 当前消息索引
|
||||||
// index: number = 0;
|
index : number = 0;
|
||||||
// // 最大消息索引
|
// 最大消息索引
|
||||||
// maxIndex: number = 999;
|
maxIndex : number = 999;
|
||||||
// // 当前终端索引
|
// 当前终端索引
|
||||||
// clientIndex: number = 99999;
|
clientIndex: number = 99999;
|
||||||
//
|
|
||||||
// connect() {
|
/**
|
||||||
// if(this.socket) {
|
* 连接信令
|
||||||
// this.socket.close();
|
*/
|
||||||
// }
|
connect() {
|
||||||
// this.connected = false;
|
if(this.socket) {
|
||||||
// hilog.info(0x0000, 'TaoyaoSignal', '连接信令:%s', setting.signalAddress);
|
hilog.info(0x0000, "TaoyaoSignal", "信令以及连接关闭旧的连接:%s", setting.signalAddress);
|
||||||
// this.socket = webSocket.createWebSocket();
|
this.socket.close();
|
||||||
// this.socket.on('open', (err: BusinessError, value: Object) => {
|
}
|
||||||
// hilog.info(0x0000, 'TaoyaoSignal', '打开信令:%s', setting.signalAddress);
|
this.connected = false;
|
||||||
// this.register();
|
hilog.info(0x0000, "TaoyaoSignal", "连接信令:%s", setting.signalAddress);
|
||||||
// this.connected = true;
|
this.socket = webSocket.createWebSocket();
|
||||||
// });
|
this.socket.on("open", (err: BusinessError, value: Object) => {
|
||||||
// this.socket.on('message', (err: BusinessError, value: string | ArrayBuffer) => {
|
hilog.info(0x0000, "TaoyaoSignal", "打开信令:%s", setting.signalAddress);
|
||||||
// hilog.debug(0x0000, 'TaoyaoSignal', '信令消息:%s', value);
|
this.register();
|
||||||
// try {
|
this.connected = true;
|
||||||
// this.onMessage(value);
|
});
|
||||||
// } catch (error) {
|
this.socket.on("message", (err: BusinessError, value: string | ArrayBuffer) => {
|
||||||
// hilog.error(0x0000, 'TaoyaoSignal', '处理信令消息异常:%s', value, error);
|
hilog.debug(0x0000, "TaoyaoSignal", "信令消息:%s", value);
|
||||||
// }
|
try {
|
||||||
// });
|
this.onMessage(value.toString());
|
||||||
// this.socket.on('close', (err: BusinessError, value: Object) => {
|
} catch (error) {
|
||||||
// hilog.error(0x0000, 'TaoyaoSignal', '关闭信令:%s', setting.signalAddress, err);
|
hilog.error(0x0000, "TaoyaoSignal", "处理信令消息异常:%s", value, error);
|
||||||
// this.reconnect();
|
}
|
||||||
// });
|
});
|
||||||
// this.socket.on('error', (err: BusinessError) => {
|
this.socket.on("close", (err: BusinessError, value: Object) => {
|
||||||
// hilog.error(0x0000, 'TaoyaoSignal', '信令异常:%s', setting.signalAddress, err);
|
hilog.error(0x0000, "TaoyaoSignal", "关闭信令:%s", setting.signalAddress, err);
|
||||||
// this.reconnect();
|
this.reconnect();
|
||||||
// });
|
});
|
||||||
// this.socket.connect(setting.signalAddress, (err: BusinessError, value: boolean) => {
|
this.socket.on("error", (err: BusinessError) => {
|
||||||
// hilog.info(0x0000, 'TaoyaoSignal', '信令连接成功:%s', setting.signalAddress);
|
hilog.error(0x0000, "TaoyaoSignal", "信令异常:%s", setting.signalAddress, err);
|
||||||
// });
|
this.reconnect();
|
||||||
// };
|
});
|
||||||
//
|
this.socket.connect(setting.signalAddress, (err: BusinessError, value: boolean) => {
|
||||||
// reconnect() {
|
hilog.info(0x0000, "TaoyaoSignal", "信令连接成功:%s", setting.signalAddress);
|
||||||
// if(!this.closed) {
|
});
|
||||||
// setTimeout(() => this.connect(), 5000);
|
};
|
||||||
// }
|
|
||||||
// }
|
/**
|
||||||
//
|
* 重连信令
|
||||||
// close() {
|
*/
|
||||||
// hilog.info(0x0000, 'TaoyaoSignal', '关闭信令:%s', setting.signalAddress);
|
reconnect() {
|
||||||
// this.closed = true;
|
if(this.closed) {
|
||||||
// this.connected = false;
|
// 已经关闭忽略重连
|
||||||
// if(this.socket) {
|
} else {
|
||||||
// this.socket.close();
|
hilog.info(0x0000, "TaoyaoSignal", "重连信令连接:%s", setting.signalAddress);
|
||||||
// }
|
setTimeout((): void => this.connect(), 5000);
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// async register() {
|
|
||||||
// const response: any = await this.request("client::register", {
|
/**
|
||||||
// name : setting.signal.name,
|
* 关闭信令
|
||||||
// clientId : setting.signal.clientId,
|
*/
|
||||||
// clientType: setting.signal.clientType,
|
close() {
|
||||||
// username : setting.signal.username,
|
hilog.info(0x0000, "TaoyaoSignal", "关闭信令:%s", setting.signalAddress);
|
||||||
// password : setting.signal.password,
|
this.closed = true;
|
||||||
// battery : 100,
|
this.connected = false;
|
||||||
// charging : true,
|
if(this.socket) {
|
||||||
// });
|
this.socket.close();
|
||||||
// const { body } = response;
|
}
|
||||||
// const { index } = body;
|
}
|
||||||
// this.clientIndex = index;
|
|
||||||
// hilog.info(0x0000, "TaoyaoSignal", "信令注册成功:%d", index);
|
/**
|
||||||
// this.heartbeat();
|
* 注册信令
|
||||||
// }
|
*/
|
||||||
//
|
async register() {
|
||||||
// heartbeat() {
|
const response: Record<string, Object> = await this.request("client::register", {
|
||||||
// if(this.heartbeatTimer) {
|
"name" : setting.signal.name,
|
||||||
// clearInterval(this.heartbeatTimer);
|
"clientId" : setting.signal.clientId,
|
||||||
// }
|
"clientType": setting.signal.clientType,
|
||||||
// this.heartbeatTimer = setInterval(() => {
|
"username" : setting.signal.username,
|
||||||
// this.send("client::heartbeat", {
|
"password" : setting.signal.password,
|
||||||
// battery : 100,
|
"battery" : 100,
|
||||||
// charging: true,
|
"charging" : true
|
||||||
// });
|
});
|
||||||
// }, 30 * 1000);
|
const body : Record<string, Object> = response.body as Record<string, Object>;
|
||||||
// };
|
const index : number = body.index as number;
|
||||||
//
|
this.clientIndex = index;
|
||||||
// buildId() {
|
hilog.info(0x0000, "TaoyaoSignal", "信令注册成功:%d", index);
|
||||||
// if (++this.index > this.maxIndex) {
|
this.heartbeat();
|
||||||
// this.index = 0;
|
}
|
||||||
// }
|
|
||||||
// const date = new Date();
|
/**
|
||||||
// return (
|
* 心跳
|
||||||
// 100000000000000 * date.getDate() +
|
*/
|
||||||
// 1000000000000 * date.getHours() +
|
heartbeat() {
|
||||||
// 10000000000 * date.getMinutes() +
|
if(this.heartbeatTimer) {
|
||||||
// 100000000 * date.getSeconds() +
|
clearInterval(this.heartbeatTimer);
|
||||||
// 1000 * this.clientIndex +
|
}
|
||||||
// this.index
|
this.heartbeatTimer = setInterval(() => {
|
||||||
// );
|
this.send("client::heartbeat", {
|
||||||
// }
|
"battery" : 100,
|
||||||
//
|
"charging": true,
|
||||||
// send(signal, body) {
|
});
|
||||||
// const header = {
|
}, 30 * 1000);
|
||||||
// v: setting.version,
|
};
|
||||||
// id: this.buildId(),
|
|
||||||
// signal
|
/**
|
||||||
// };
|
* @returns ID
|
||||||
// const message = {
|
*/
|
||||||
// header,
|
buildId(): number {
|
||||||
// body
|
if (++this.index > this.maxIndex) {
|
||||||
// };
|
this.index = 0;
|
||||||
// try {
|
}
|
||||||
// this.socket.send(JSON.stringify(message));
|
const date = new Date();
|
||||||
// } catch (error) {
|
return (
|
||||||
// hilog.error(0x0000, 'TaoyaoSignal', '发送消息异常:%o', message, error);
|
100000000000000 * date.getDate() +
|
||||||
// }
|
1000000000000 * date.getHours() +
|
||||||
// }
|
10000000000 * date.getMinutes() +
|
||||||
//
|
100000000 * date.getSeconds() +
|
||||||
// async request(signal, body) {
|
1000 * this.clientIndex +
|
||||||
// const id = this.buildId();
|
this.index
|
||||||
// return new Promise((resolve, reject) => {
|
);
|
||||||
// const header = {
|
}
|
||||||
// v: setting.version,
|
|
||||||
// id,
|
/**
|
||||||
// signal
|
* 发送消息
|
||||||
// };
|
*
|
||||||
// const message = {
|
* @param signal 信令
|
||||||
// header,
|
* @param body 主体
|
||||||
// body
|
*/
|
||||||
// };
|
send(signal: string, body: Record<string, Object>) {
|
||||||
// // 设置超时
|
const header: Record<string, Object> = {
|
||||||
// const rejectTimeout = setTimeout(() => {
|
"v" : setting.version,
|
||||||
// this.callbackMapping.delete(id);
|
"id" : this.buildId(),
|
||||||
// reject("请求超时");
|
"signal": signal
|
||||||
// }, 5000);
|
};
|
||||||
// // 请求回调
|
const message: Record<string, Object> = {
|
||||||
// this.callbackMapping.set(id, (response) => {
|
"header": header,
|
||||||
// resolve(response);
|
"body" : body
|
||||||
// clearTimeout(rejectTimeout);
|
};
|
||||||
// // 默认不用继续处理
|
try {
|
||||||
// return true;
|
this.socket?.send(JSON.stringify(message));
|
||||||
// });
|
} catch (error) {
|
||||||
// // 发送消息
|
hilog.error(0x0000, "TaoyaoSignal", "发送消息异常:%o", message, error);
|
||||||
// try {
|
}
|
||||||
// this.socket.send(JSON.stringify(message));
|
}
|
||||||
// } catch (error) {
|
|
||||||
// hilog.error(0x0000, 'TaoyaoSignal', '发送消息异常:%o', message, error);
|
/**
|
||||||
// reject(error);
|
* 请求消息
|
||||||
// }
|
*
|
||||||
// });
|
* @param signal 信令
|
||||||
// }
|
* @param body 主体
|
||||||
//
|
*
|
||||||
// onMessage(message) {
|
* @returns 响应
|
||||||
// const json = JSON.parse(message);
|
*/
|
||||||
// const {
|
async request(signal: string, body: Record<string, Object>): Promise<Record<string, Object>> {
|
||||||
// header,
|
const id = this.buildId();
|
||||||
// body,
|
return new Promise<Record<string, Object>>((resolve, reject) => {
|
||||||
// } = json;
|
const header: Record<string, Object> = {
|
||||||
// const {
|
"v" : setting.version,
|
||||||
// id
|
"id" : id,
|
||||||
// } = header;
|
"signal": signal
|
||||||
// if (this.callbackMapping.has(id)) {
|
};
|
||||||
// hilog.info(0x0000, "TaoyaoSignal", "处理同步消息:%s", message);
|
const message: Record<string, Object> = {
|
||||||
// try {
|
"header": header,
|
||||||
// if(
|
"body" : body
|
||||||
// this.callbackMapping.get(id)(json)
|
};
|
||||||
// ) {
|
// 设置超时
|
||||||
// return;
|
const rejectTimeout = setTimeout(() => {
|
||||||
// }
|
this.callbackMapping.delete(id);
|
||||||
// } finally {
|
reject("请求超时");
|
||||||
// this.callbackMapping.delete(id);
|
}, 5000);
|
||||||
// }
|
// 请求回调
|
||||||
// }
|
this.callbackMapping.set(id, (response: Record<string, Object>) => {
|
||||||
// hilog.info(0x0000, "TaoyaoSignal", "处理异步消息:%s", message);
|
resolve(response);
|
||||||
// // TODO
|
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<string, Object> = JSON.parse(message);
|
||||||
|
const header: Record<string, Object> = json.header as Record<string, Object>;
|
||||||
|
const body : Record<string, Object> = json.body as Record<string, Object>;
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user