[*] 连接信令
This commit is contained in:
@@ -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": {}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libtaoyao.so",
|
||||
"types": "./index.d.ts",
|
||||
"version": "",
|
||||
"version": "1.0.0",
|
||||
"description": "桃夭媒体依赖"
|
||||
}
|
||||
@@ -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%');
|
||||
|
||||
@@ -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<T = void> 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<number, Function>();
|
||||
// 当前消息索引
|
||||
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<string, Object> = 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<string, Object> = response.body as Record<string, Object>;
|
||||
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<string, Object>) {
|
||||
const header: Record<string, Object> = {
|
||||
"v" : setting.version,
|
||||
"id" : this.buildId(),
|
||||
"signal": signal
|
||||
};
|
||||
const message: Record<string, Object> = {
|
||||
"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<string, Object>): Promise<Record<string, Object>> {
|
||||
const id = this.buildId();
|
||||
return new Promise<Record<string, Object>>((resolve, reject) => {
|
||||
const header: Record<string, Object> = {
|
||||
"v" : setting.version,
|
||||
"id" : id,
|
||||
"signal": signal
|
||||
};
|
||||
const message: Record<string, Object> = {
|
||||
"header": header,
|
||||
"body" : body
|
||||
};
|
||||
// 设置超时
|
||||
const rejectTimeout = setTimeout(() => {
|
||||
this.callbackMapping.delete(id);
|
||||
reject("请求超时");
|
||||
}, 5000);
|
||||
// 请求回调
|
||||
this.callbackMapping.set(id, (response: Record<string, Object>) => {
|
||||
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<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