[*] 日常优化

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

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

View File

@@ -12,9 +12,9 @@ import {
*/
const protocol = {
// 当前索引
index: 0,
index : 0,
// 最大索引
maxIndex: 999,
maxIndex : 999,
// 终端索引
clientIndex: 99999,
/**
@@ -44,13 +44,14 @@ const protocol = {
* @returns 信令消息
*/
buildMessage(signal, body = {}, id, v) {
const me = this;
const message = {
header: {
v: v || "1.0.0",
id: id || this.buildId(),
v : v || "1.0.0",
id : id || me.buildId(),
signal: signal,
},
body: body,
body: body,
};
return message;
},
@@ -65,28 +66,28 @@ const taoyaoProtocol = protocol;
* 信令通道
*/
const signalChannel = {
// 桃夭
taoyao: null,
// 通道
// 桃夭信令
taoyao : null,
// 信令通道
channel: null,
// 地址
// 信令地址
address: null,
// 心跳时间
heartbeatTime: 30 * 1000,
heartbeatTime : 30 * 1000,
// 心跳定时器
heartbeatTimer: null,
// 是否重连
reconnection: true,
reconnection : true,
// 防止重复重连
lockReconnect : false,
// 重连定时器
reconnectTimer: null,
// 防止重复重连
lockReconnect: false,
// 当前重连时间
reconnectionTimeout: 5 * 1000,
reconnectionTimeout : 5 * 1000,
// 最小重连时间
minReconnectionDelay: 5 * 1000,
// 最大重连时间
maxReconnectionDelay: 60 * 1000,
maxReconnectionDelay: 30 * 1000,
/**
* 心跳
*/
@@ -100,13 +101,13 @@ const signalChannel = {
const battery = await navigator.getBattery();
me.push(
protocol.buildMessage("client::heartbeat", {
battery: battery.level * 100,
battery : battery.level * 100,
charging: battery.charging,
})
);
me.heartbeat();
} else {
console.warn("心跳失败", me.address);
console.warn("心跳失败", me.address);
}
}, me.heartbeatTime);
},
@@ -118,9 +119,9 @@ const signalChannel = {
return me.channel && me.channel.readyState === WebSocket.OPEN;
},
/**
* 连接
* 连接信令
*
* @param {*} address 地址
* @param {*} address 信令地址
* @param {*} reconnection 是否重连
*
* @returns Promise
@@ -132,32 +133,32 @@ const signalChannel = {
resolve(me.channel);
});
}
me.address = address;
me.address = address;
me.reconnection = reconnection;
return new Promise((resolve, reject) => {
console.debug("连接信令通道", me.address);
console.debug("连接信令通道", me.address);
me.channel = new WebSocket(me.address);
me.channel.onopen = async function () {
console.debug("打开信令通道", me.address);
console.info("打开信令通道", me.address);
const battery = await navigator.getBattery();
me.push(
protocol.buildMessage("client::register", {
name: me.taoyao.name,
clientId: me.taoyao.clientId,
name : me.taoyao.name,
clientId : me.taoyao.clientId,
clientType: "WEB",
username: me.taoyao.username,
password: me.taoyao.password,
battery: battery.level * 100,
charging: battery.charging,
username : me.taoyao.username,
password : me.taoyao.password,
battery : battery.level * 100,
charging : battery.charging,
})
);
me.reconnectionTimeout = me.minReconnectionDelay;
me.taoyao.connect = true;
me.taoyao.connect = true;
me.heartbeat();
resolve(me.channel);
};
me.channel.onclose = async function () {
console.warn("信令通道关闭", me.channel);
console.warn("信令通道关闭", me.channel);
me.taoyao.connect = false;
if(!me.connected()) {
me.taoyao.closeRoomMedia();
@@ -169,21 +170,22 @@ const signalChannel = {
// 不要失败回调
};
me.channel.onerror = async function (e) {
console.error("信令通道异常", me.channel, e);
console.error("信令通道异常", me.channel, e);
// 不要失败回调
};
me.channel.onmessage = async function (e) {
const content = e.data;
try {
console.debug("信令通道消息", e.data);
me.taoyao.on(JSON.parse(e.data));
console.debug("信令通道消息", content);
me.taoyao.on(JSON.parse(content));
} catch (error) {
console.error("处理信令消息异常", e, error);
console.error("处理信令通道消息异常", e, error);
}
};
});
},
/**
* 重连
* 重连信令
*/
reconnect() {
const me = this;
@@ -200,7 +202,7 @@ const signalChannel = {
}
// 定时重连
me.reconnectTimer = setTimeout(function () {
console.info("重连信令通道", me.address);
console.info("重连信令通道", me.address);
me.connect(me.address, me.reconnection);
me.lockReconnect = false;
}, me.reconnectionTimeout);
@@ -215,10 +217,11 @@ const signalChannel = {
* @param {*} message 消息
*/
push(message) {
const me = this;
try {
signalChannel.channel.send(JSON.stringify(message));
me.channel.send(JSON.stringify(message));
} catch (error) {
console.error("异步请求异常", message, error);
console.error("异步请求异常", message, error);
}
},
/**

View File

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