[*] 资源释放优化

This commit is contained in:
acgist
2023-04-30 13:42:58 +08:00
parent 165655c4e6
commit d9915e3013
6 changed files with 106 additions and 66 deletions

View File

@@ -91,7 +91,7 @@ const signalChannel = {
clearTimeout(me.heartbeatTimer);
}
me.heartbeatTimer = setTimeout(async function () {
if (me.channel && me.channel.readyState === WebSocket.OPEN) {
if (me.connected()) {
me.push(
// TODO电池信息
protocol.buildMessage("client::heartbeat", {
@@ -115,7 +115,7 @@ const signalChannel = {
*/
async connect(address, reconnection = true) {
const me = this;
if (me.channel && me.channel.readyState === WebSocket.OPEN) {
if (me.connected()) {
return new Promise((resolve, reject) => {
resolve(me.channel);
});
@@ -147,7 +147,7 @@ const signalChannel = {
me.channel.on("close", async function () {
console.warn("信令通道关闭:", me.address);
me.taoyao.connect = false;
if(me.channel && me.channel.readyState !== WebSocket.OPEN) {
if(!me.connected()) {
me.taoyao.closeAllRoom();
}
if (me.reconnection) {
@@ -157,13 +157,6 @@ const signalChannel = {
});
me.channel.on("error", async function (e) {
console.error("信令通道异常:", me.address, e);
me.taoyao.connect = false;
if(me.channel && me.channel.readyState !== WebSocket.OPEN) {
me.taoyao.closeAllRoom();
}
if (me.reconnection) {
me.reconnect();
}
// 不要失败回调
});
me.channel.on("message", async function (data) {
@@ -177,14 +170,22 @@ const signalChannel = {
});
});
},
/**
* @returns 是否连接成功
*/
connected() {
const me = this;
return me.channel && me.channel.readyState === WebSocket.OPEN;
},
/**
* 重连
*/
reconnect() {
const me = this;
if (
me.lockReconnect ||
(me.channel && me.channel.readyState === WebSocket.OPEN)
me.lockReconnect ||
me.taoyao.connect ||
me.connected()
) {
return;
}
@@ -551,7 +552,7 @@ class Taoyao {
}
closeAllRoom() {
console.info("关闭所有房间");
console.info("关闭所有房间", this.rooms.size());
this.rooms.forEach((room, roomId) => {
room.closeAll();
});