[*] 日常优化

This commit is contained in:
acgist
2023-06-25 08:07:51 +08:00
parent b34c2e904b
commit c4677fb246
3 changed files with 100 additions and 92 deletions

View File

@@ -17,8 +17,9 @@ const protocol = {
* @returns 索引 * @returns 索引
*/ */
buildId() { buildId() {
if (++this.index > this.maxIndex) { const me = this;
this.index = 0; if (++me.index > me.maxIndex) {
me.index = 0;
} }
const date = new Date(); const date = new Date();
return ( return (
@@ -26,8 +27,8 @@ const protocol = {
1000000000000 * date.getHours() + 1000000000000 * date.getHours() +
10000000000 * date.getMinutes() + 10000000000 * date.getMinutes() +
100000000 * date.getSeconds() + 100000000 * date.getSeconds() +
1000 * this.clientIndex + 1000 * me.clientIndex +
this.index me.index
); );
}, },
/** /**
@@ -39,10 +40,11 @@ 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 || this.buildId(), id : id || me.buildId(),
signal: signal, signal: signal,
}, },
body: body, body: body,
@@ -60,11 +62,11 @@ const protocol = {
* 信令通道 * 信令通道
*/ */
const signalChannel = { const signalChannel = {
// 桃夭 // 桃夭信令
taoyao : null, taoyao : null,
// 通道 // 信令通道
channel: null, channel: null,
// 地址 // 信令地址
address: null, address: null,
// 心跳时间 // 心跳时间
heartbeatTime : 30 * 1000, heartbeatTime : 30 * 1000,
@@ -72,16 +74,16 @@ const signalChannel = {
heartbeatTimer: null, heartbeatTimer: null,
// 是否重连 // 是否重连
reconnection : true, reconnection : true,
// 重连定时器
reconnectTimer: null,
// 防止重复重连 // 防止重复重连
lockReconnect : false, lockReconnect : false,
// 重连定时器
reconnectTimer: null,
// 当前重连时间 // 当前重连时间
reconnectionTimeout : 5 * 1000, reconnectionTimeout : 5 * 1000,
// 最小重连时间 // 最小重连时间
minReconnectionDelay: 5 * 1000, minReconnectionDelay: 5 * 1000,
// 最大重连时间 // 最大重连时间
maxReconnectionDelay: 60 * 1000, maxReconnectionDelay: 30 * 1000,
/** /**
* 心跳 * 心跳
*/ */
@@ -93,22 +95,29 @@ const signalChannel = {
me.heartbeatTimer = setTimeout(async function () { me.heartbeatTimer = setTimeout(async function () {
if (me.connected()) { if (me.connected()) {
me.push( me.push(
// TODO电池信息
protocol.buildMessage("client::heartbeat", { protocol.buildMessage("client::heartbeat", {
// TODO电池信息
battery : 100, battery : 100,
charging: true, charging: true,
}) })
); );
me.heartbeat(); me.heartbeat();
} else { } else {
console.warn("心跳失败", me.address); console.warn("心跳失败", me.address);
} }
}, me.heartbeatTime); }, me.heartbeatTime);
}, },
/** /**
* 连接 * @returns 是否连接成功
*/
connected() {
const me = this;
return me.channel && me.channel.readyState === WebSocket.OPEN;
},
/**
* 连接信令
* *
* @param {*} address 地址 * @param {*} address 信令地址
* @param {*} reconnection 是否重连 * @param {*} reconnection 是否重连
* *
* @returns Promise * @returns Promise
@@ -123,11 +132,10 @@ const signalChannel = {
me.address = address; me.address = address;
me.reconnection = reconnection; me.reconnection = reconnection;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.debug("连接信令通道", me.address); console.debug("连接信令通道", me.address);
me.channel = new WebSocket(me.address, { rejectUnauthorized: false, handshakeTimeout: 5000 }); me.channel = new WebSocket(me.address, { rejectUnauthorized: false, handshakeTimeout: 5000 });
me.channel.on("open", async function () { me.channel.on("open", async function () {
console.info("打开信令通道", me.address); console.info("打开信令通道", me.address);
// TODO电池信息
me.push( me.push(
protocol.buildMessage("client::register", { protocol.buildMessage("client::register", {
name : config.signal.name, name : config.signal.name,
@@ -135,6 +143,7 @@ const signalChannel = {
clientType: "MEDIA", clientType: "MEDIA",
username : config.signal.username, username : config.signal.username,
password : config.signal.password, password : config.signal.password,
// TODO电池信息
battery : 100, battery : 100,
charging : true, charging : true,
}) })
@@ -145,7 +154,7 @@ const signalChannel = {
resolve(me.channel); resolve(me.channel);
}); });
me.channel.on("close", async function () { me.channel.on("close", async function () {
console.warn("信令通道关闭", me.address); console.warn("信令通道关闭", me.address);
me.taoyao.connect = false; me.taoyao.connect = false;
if(!me.connected()) { if(!me.connected()) {
me.taoyao.closeAllRoom(); me.taoyao.closeAllRoom();
@@ -156,29 +165,22 @@ const signalChannel = {
// 不要失败回调 // 不要失败回调
}); });
me.channel.on("error", async function (e) { me.channel.on("error", async function (e) {
console.error("信令通道异常", me.address, e); console.error("信令通道异常", me.address, e);
// 不要失败回调 // 不要失败回调
}); });
me.channel.on("message", async function (data) { me.channel.on("message", async function (data) {
try {
const content = data.toString(); const content = data.toString();
console.debug("信令通道消息:", content); try {
console.debug("信令通道消息", content);
me.taoyao.on(JSON.parse(content)); me.taoyao.on(JSON.parse(content));
} catch (error) { } catch (error) {
console.error("处理信令消息异常", data.toString(), error); console.error("处理信令通道消息异常", content, error);
} }
}); });
}); });
}, },
/** /**
* @returns 是否连接成功 * 重连信令
*/
connected() {
const me = this;
return me.channel && me.channel.readyState === WebSocket.OPEN;
},
/**
* 重连
*/ */
reconnect() { reconnect() {
const me = this; const me = this;
@@ -195,7 +197,7 @@ const signalChannel = {
} }
// 定时重连 // 定时重连
me.reconnectTimer = setTimeout(function () { me.reconnectTimer = setTimeout(function () {
console.info("重连信令通道", me.address); console.info("重连信令通道", me.address);
me.connect(me.address, me.reconnection); me.connect(me.address, me.reconnection);
me.lockReconnect = false; me.lockReconnect = false;
}, me.reconnectionTimeout); }, me.reconnectionTimeout);
@@ -210,10 +212,11 @@ const signalChannel = {
* @param {*} message 消息 * @param {*} message 消息
*/ */
push(message) { push(message) {
const me = this;
try { try {
this.channel.send(JSON.stringify(message)); me.channel.send(JSON.stringify(message));
} catch (error) { } catch (error) {
console.error("异步请求异常", message, error); console.error("异步请求异常", message, error);
} }
}, },
/** /**
@@ -229,6 +232,8 @@ const signalChannel = {
}, },
}; };
// TODO: continue
/** /**
* 房间 * 房间
*/ */

View File

@@ -44,10 +44,11 @@ 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 || "1.0.0", v : v || "1.0.0",
id: id || this.buildId(), id : id || me.buildId(),
signal: signal, signal: signal,
}, },
body: body, body: body,
@@ -65,11 +66,11 @@ const taoyaoProtocol = protocol;
* 信令通道 * 信令通道
*/ */
const signalChannel = { const signalChannel = {
// 桃夭 // 桃夭信令
taoyao : null, taoyao : null,
// 通道 // 信令通道
channel: null, channel: null,
// 地址 // 信令地址
address: null, address: null,
// 心跳时间 // 心跳时间
heartbeatTime : 30 * 1000, heartbeatTime : 30 * 1000,
@@ -77,16 +78,16 @@ const signalChannel = {
heartbeatTimer: null, heartbeatTimer: null,
// 是否重连 // 是否重连
reconnection : true, reconnection : true,
// 重连定时器
reconnectTimer: null,
// 防止重复重连 // 防止重复重连
lockReconnect : false, lockReconnect : false,
// 重连定时器
reconnectTimer: null,
// 当前重连时间 // 当前重连时间
reconnectionTimeout : 5 * 1000, reconnectionTimeout : 5 * 1000,
// 最小重连时间 // 最小重连时间
minReconnectionDelay: 5 * 1000, minReconnectionDelay: 5 * 1000,
// 最大重连时间 // 最大重连时间
maxReconnectionDelay: 60 * 1000, maxReconnectionDelay: 30 * 1000,
/** /**
* 心跳 * 心跳
*/ */
@@ -106,7 +107,7 @@ const signalChannel = {
); );
me.heartbeat(); me.heartbeat();
} else { } else {
console.warn("心跳失败", me.address); console.warn("心跳失败", me.address);
} }
}, me.heartbeatTime); }, me.heartbeatTime);
}, },
@@ -118,9 +119,9 @@ const signalChannel = {
return me.channel && me.channel.readyState === WebSocket.OPEN; return me.channel && me.channel.readyState === WebSocket.OPEN;
}, },
/** /**
* 连接 * 连接信令
* *
* @param {*} address 地址 * @param {*} address 信令地址
* @param {*} reconnection 是否重连 * @param {*} reconnection 是否重连
* *
* @returns Promise * @returns Promise
@@ -135,10 +136,10 @@ const signalChannel = {
me.address = address; me.address = address;
me.reconnection = reconnection; me.reconnection = reconnection;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.debug("连接信令通道", me.address); console.debug("连接信令通道", me.address);
me.channel = new WebSocket(me.address); me.channel = new WebSocket(me.address);
me.channel.onopen = async function () { me.channel.onopen = async function () {
console.debug("打开信令通道", me.address); console.info("打开信令通道", me.address);
const battery = await navigator.getBattery(); const battery = await navigator.getBattery();
me.push( me.push(
protocol.buildMessage("client::register", { protocol.buildMessage("client::register", {
@@ -157,7 +158,7 @@ const signalChannel = {
resolve(me.channel); resolve(me.channel);
}; };
me.channel.onclose = async function () { me.channel.onclose = async function () {
console.warn("信令通道关闭", me.channel); console.warn("信令通道关闭", me.channel);
me.taoyao.connect = false; me.taoyao.connect = false;
if(!me.connected()) { if(!me.connected()) {
me.taoyao.closeRoomMedia(); me.taoyao.closeRoomMedia();
@@ -169,21 +170,22 @@ const signalChannel = {
// 不要失败回调 // 不要失败回调
}; };
me.channel.onerror = async function (e) { me.channel.onerror = async function (e) {
console.error("信令通道异常", me.channel, e); console.error("信令通道异常", me.channel, e);
// 不要失败回调 // 不要失败回调
}; };
me.channel.onmessage = async function (e) { me.channel.onmessage = async function (e) {
const content = e.data;
try { try {
console.debug("信令通道消息", e.data); console.debug("信令通道消息", content);
me.taoyao.on(JSON.parse(e.data)); me.taoyao.on(JSON.parse(content));
} catch (error) { } catch (error) {
console.error("处理信令消息异常", e, error); console.error("处理信令通道消息异常", e, error);
} }
}; };
}); });
}, },
/** /**
* 重连 * 重连信令
*/ */
reconnect() { reconnect() {
const me = this; const me = this;
@@ -200,7 +202,7 @@ const signalChannel = {
} }
// 定时重连 // 定时重连
me.reconnectTimer = setTimeout(function () { me.reconnectTimer = setTimeout(function () {
console.info("重连信令通道", me.address); console.info("重连信令通道", me.address);
me.connect(me.address, me.reconnection); me.connect(me.address, me.reconnection);
me.lockReconnect = false; me.lockReconnect = false;
}, me.reconnectionTimeout); }, me.reconnectionTimeout);
@@ -215,10 +217,11 @@ const signalChannel = {
* @param {*} message 消息 * @param {*} message 消息
*/ */
push(message) { push(message) {
const me = this;
try { try {
signalChannel.channel.send(JSON.stringify(message)); me.channel.send(JSON.stringify(message));
} catch (error) { } catch (error) {
console.error("异步请求异常", message, error); console.error("异步请求异常", message, error);
} }
}, },
/** /**

View File

@@ -11,6 +11,9 @@ server:
port-header: X-Forwarded-Port port-header: X-Forwarded-Port
protocol-header: X-Forwarded-Proto protocol-header: X-Forwarded-Proto
remote-ip-header: X-Forwarded-For remote-ip-header: X-Forwarded-For
# 服务前缀
# servlet:
# context-path: /taoyao
spring: spring:
# 快速启动 # 快速启动
# main: # main:
@@ -262,6 +265,3 @@ taoyao:
# - network: 192.168.8.0 # - network: 192.168.8.0
# inner-host: # inner-host:
# outer-host: # outer-host:
# 服务前缀
# servlet:
# context-path: /taoyao