[*] 日常优化

This commit is contained in:
acgist
2023-12-21 08:00:57 +08:00
parent 7816a53dae
commit 07d0be0020
3 changed files with 96 additions and 86 deletions

View File

@@ -2,11 +2,11 @@ import * as mediasoupClient from "mediasoup-client";
import { import {
config, config,
defaultAudioConfig, defaultAudioConfig,
defaultVideoConfig,
defaultShareScreenConfig,
defaultSvcEncodings,
defaultSimulcastEncodings,
defaultRTCPeerConnectionConfig, defaultRTCPeerConnectionConfig,
defaultShareScreenConfig,
defaultSimulcastEncodings,
defaultSvcEncodings,
defaultVideoConfig,
} from "./Config.js"; } from "./Config.js";
/** /**
@@ -22,19 +22,20 @@ const SUCCESS_MESSAGE = "成功";
* 信令协议 * 信令协议
*/ */
const protocol = { const protocol = {
// 当前索引 // 当前索引
index : 0, index : 0,
// 最大索引 // 最大索引
maxIndex : 999, maxIndex : 999,
// 终端索引 // 终端索引
clientIndex: 99999, clientIndex: 99999,
/** /**
* @returns 索引 * @returns 索引
*/ */
buildId() { buildId() {
const me = this; if (++this.index > this.maxIndex) {
if (++me.index > me.maxIndex) { this.index = 0;
me.index = 0;
} }
const date = new Date(); const date = new Date();
return ( return (
@@ -42,10 +43,11 @@ const protocol = {
1000000000000 * date.getHours() + 1000000000000 * date.getHours() +
10000000000 * date.getMinutes() + 10000000000 * date.getMinutes() +
100000000 * date.getSeconds() + 100000000 * date.getSeconds() +
1000 * me.clientIndex + 1000 * this.clientIndex +
me.index this.index
); );
}, },
/** /**
* @param {*} signal 信令标识 * @param {*} signal 信令标识
* @param {*} body 消息主体 * @param {*} body 消息主体
@@ -55,11 +57,10 @@ const protocol = {
* @returns 信令消息 * @returns 信令消息
*/ */
buildMessage(signal, body = {}, id, v) { buildMessage(signal, body = {}, id, v) {
const me = this;
const message = { const message = {
header: { header: {
v : v || config.signal.version, v : v || config.signal.version,
id : id || me.buildId(), id : id || this.buildId(),
signal: signal, signal: signal,
}, },
body: body, body: body,
@@ -77,6 +78,7 @@ const taoyaoProtocol = protocol;
* 信令通道 * 信令通道
*/ */
const signalChannel = { const signalChannel = {
// 桃夭信令 // 桃夭信令
taoyao : null, taoyao : null,
// 信令通道 // 信令通道
@@ -99,34 +101,35 @@ const signalChannel = {
minReconnectionDelay: 5 * 1000, minReconnectionDelay: 5 * 1000,
// 最大重连时间 // 最大重连时间
maxReconnectionDelay: 30 * 1000, maxReconnectionDelay: 30 * 1000,
/** /**
* 心跳 * 心跳
*/ */
heartbeat() { heartbeat() {
const me = this; if (this.heartbeatTimer) {
if (me.heartbeatTimer) { clearTimeout(this.heartbeatTimer);
clearTimeout(me.heartbeatTimer);
} }
me.heartbeatTimer = setTimeout(async () => { this.heartbeatTimer = setTimeout(async () => {
if (me.connected()) { if (this.connected()) {
const battery = await navigator.getBattery(); const battery = await navigator.getBattery();
me.taoyao.push(protocol.buildMessage("client::heartbeat", { this.taoyao.push(protocol.buildMessage("client::heartbeat", {
battery : battery.level * 100, battery : battery.level * 100,
charging: battery.charging, charging: battery.charging,
})); }));
me.heartbeat(); this.heartbeat();
} else { } else {
console.warn("心跳失败", me.address); console.warn("心跳失败", this.address);
} }
}, me.heartbeatTime); }, this.heartbeatTime);
}, },
/** /**
* @returns 是否连接成功 * @returns 是否连接成功
*/ */
connected() { connected() {
const me = this; return this.channel && this.channel.readyState === WebSocket.OPEN;
return me.channel && me.channel.readyState === WebSocket.OPEN;
}, },
/** /**
* 连接信令 * 连接信令
* *
@@ -136,105 +139,102 @@ const signalChannel = {
* @returns Promise<WebSocket> * @returns Promise<WebSocket>
*/ */
async connect(address, reconnection = true) { async connect(address, reconnection = true) {
const me = this; if (this.connected()) {
if (me.connected()) {
this.taoyao.connect = true; this.taoyao.connect = true;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve(me.channel); resolve(this.channel);
}); });
} else { } else {
this.taoyao.connect = false; this.taoyao.connect = false;
} }
me.address = address; this.address = address;
me.reconnection = reconnection; this.reconnection = reconnection;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.debug("连接信令通道", me.address); console.debug("连接信令通道", this.address);
me.channel = new WebSocket(me.address); this.channel = new WebSocket(this.address);
me.channel.onopen = async () => { this.channel.onopen = async () => {
console.info("打开信令通道", me.address); console.debug("打开信令通道", this.address);
const battery = await navigator.getBattery(); const battery = await navigator.getBattery();
const { body } = await me.taoyao.request(protocol.buildMessage("client::register", { const {
name : me.taoyao.name, body
clientId : me.taoyao.clientId, } = await this.taoyao.request(protocol.buildMessage("client::register", {
name : this.taoyao.name,
clientId : this.taoyao.clientId,
clientType: config.signal.clientType, clientType: config.signal.clientType,
username : me.taoyao.username, username : this.taoyao.username,
password : me.taoyao.password, password : this.taoyao.password,
battery : battery.level * 100, battery : battery.level * 100,
charging : battery.charging, charging : battery.charging,
})); }));
protocol.clientIndex = body.index; protocol.clientIndex = body.index;
console.info("终端注册成功", protocol.clientIndex); this.taoyao.connect = true;
me.reconnectionTimeout = me.minReconnectionDelay; this.reconnectionTimeout = this.minReconnectionDelay;
me.taoyao.connect = true; console.debug("终端注册成功", protocol.clientIndex);
me.heartbeat(); this.heartbeat();
resolve(me.channel); resolve(this.channel);
}; };
me.channel.onclose = async () => { this.channel.onclose = async () => {
console.warn("信令通道关闭", me.channel); console.warn("信令通道关闭", this.channel);
me.taoyao.connect = false; this.taoyao.connect = false;
if(!me.connected()) { await this.taoyao.closeRoomMedia();
await me.taoyao.closeRoomMedia(); await this.taoyao.closeSessionMedia();
await me.taoyao.closeSessionMedia(); if (this.reconnection) {
} this.reconnect();
if (me.reconnection) {
me.reconnect();
} }
// 不要失败回调 // 不要失败回调
}; };
me.channel.onerror = async (e) => { this.channel.onerror = async (e) => {
console.error("信令通道异常", me.channel, e); console.error("信令通道异常", this.channel, e);
// 不要失败回调 // 不要失败回调
}; };
me.channel.onmessage = async (e) => { this.channel.onmessage = async (e) => {
const content = e.data; const content = e.data;
try { try {
console.debug("信令通道消息", content); console.debug("信令通道消息", content);
me.taoyao.on(JSON.parse(content)); this.taoyao.on(JSON.parse(content));
} catch (error) { } catch (error) {
console.error("处理信令通道消息异常", e, error); console.error("处理信令通道消息异常", e, error);
} }
}; };
}); });
}, },
/** /**
* 重连信令 * 重连信令
*/ */
reconnect() { reconnect() {
const me = this; if (this.connected() || this.lockReconnect) {
if (
me.lockReconnect ||
me.taoyao.connect ||
me.connected()
) {
return; return;
} }
me.lockReconnect = true; this.lockReconnect = true;
if (me.reconnectTimer) { if (this.reconnectTimer) {
clearTimeout(me.reconnectTimer); clearTimeout(this.reconnectTimer);
} }
// 定时重连 // 定时重连
me.reconnectTimer = setTimeout(() => { this.reconnectTimer = setTimeout(() => {
console.info("重连信令通道", me.address); console.debug("重连信令通道", this.address);
me.connect(me.address, me.reconnection); this.connect(this.address, this.reconnection);
me.lockReconnect = false; this.lockReconnect = false;
}, me.reconnectionTimeout); }, this.reconnectionTimeout);
me.reconnectionTimeout = Math.min( // 设置重连时间
me.reconnectionTimeout + me.minReconnectionDelay, this.reconnectionTimeout = Math.min(
me.maxReconnectionDelay this.reconnectionTimeout + this.minReconnectionDelay,
this.maxReconnectionDelay
); );
}, },
/** /**
* 关闭通道 * 关闭通道
*/ */
close() { close() {
const me = this; console.debug("关闭信令通道", this.address);
console.info("关闭信令通道", me.address); clearTimeout(this.heartbeatTimer);
clearTimeout(me.heartbeatTimer); clearTimeout(this.reconnectTimer);
clearTimeout(me.reconnectTimer); this.reconnection = false;
me.reconnection = false; this.taoyao.connect = false;
me.taoyao.connect = false; this.channel.close();
me.channel.close();
}, },
}; };
/** /**
@@ -655,6 +655,7 @@ class Taoyao extends RemoteClient {
*/ */
async connectSignal(callback) { async connectSignal(callback) {
const me = this; const me = this;
this.closed = false;
me.callback = callback; me.callback = callback;
signalChannel.taoyao = me; signalChannel.taoyao = me;
return await signalChannel.connect( return await signalChannel.connect(
@@ -3937,6 +3938,8 @@ class Taoyao extends RemoteClient {
await this.closeSessionMedia(); await this.closeSessionMedia();
signalChannel.close(); signalChannel.close();
} }
} }
export { Taoyao }; export { Taoyao };

View File

@@ -4,13 +4,13 @@ import { createApp } from "vue";
import "./assets/main.css"; import "./assets/main.css";
import "element-plus/dist/index.css"; import "element-plus/dist/index.css";
const app = createApp(App);
app.use(ElementPlus);
app.mount("#app");
console.info(` console.info(`
中庭地白树栖鸦,冷露无声湿桂花。 中庭地白树栖鸦,冷露无声湿桂花。
今夜月明人尽望,不知秋思落谁家。 今夜月明人尽望,不知秋思落谁家。
:: https://gitee.com/acgist/taoyao :: https://gitee.com/acgist/taoyao
`); `);
const app = createApp(App);
app.use(ElementPlus);
app.mount("#app");

View File

@@ -4,7 +4,6 @@ import { defineConfig } from "vite";
import { fileURLToPath, URL } from "node:url"; import { fileURLToPath, URL } from "node:url";
export default defineConfig({ export default defineConfig({
plugins: [vue()],
server: { server: {
port : 8443, port : 8443,
host : "0.0.0.0", host : "0.0.0.0",
@@ -13,9 +12,17 @@ export default defineConfig({
cert: fs.readFileSync("src/certs/server.crt"), cert: fs.readFileSync("src/certs/server.crt"),
}, },
}, },
plugins: [ vue() ],
resolve: { resolve: {
alias: { alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)), "@": fileURLToPath(new URL("./src", import.meta.url)),
}, },
}, },
}); });
console.info(`
中庭地白树栖鸦,冷露无声湿桂花。
今夜月明人尽望,不知秋思落谁家。
:: https://gitee.com/acgist/taoyao
`);