[*] 日常优化
This commit is contained in:
@@ -8,17 +8,18 @@ const WebSocket = require("ws");
|
|||||||
*/
|
*/
|
||||||
const protocol = {
|
const protocol = {
|
||||||
// 当前索引
|
// 当前索引
|
||||||
index: 0,
|
index : 0,
|
||||||
// 最大索引
|
// 最大索引
|
||||||
maxIndex: 999,
|
maxIndex : 999,
|
||||||
// 终端索引
|
// 终端索引
|
||||||
clientIndex: 99999,
|
clientIndex: 99999,
|
||||||
/**
|
/**
|
||||||
* @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,13 +40,14 @@ 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,
|
||||||
};
|
};
|
||||||
return message;
|
return message;
|
||||||
},
|
},
|
||||||
@@ -60,28 +62,28 @@ const protocol = {
|
|||||||
* 信令通道
|
* 信令通道
|
||||||
*/
|
*/
|
||||||
const signalChannel = {
|
const signalChannel = {
|
||||||
// 桃夭
|
// 桃夭信令
|
||||||
taoyao: null,
|
taoyao : null,
|
||||||
// 通道
|
// 信令通道
|
||||||
channel: null,
|
channel: null,
|
||||||
// 地址
|
// 信令地址
|
||||||
address: null,
|
address: null,
|
||||||
// 心跳时间
|
// 心跳时间
|
||||||
heartbeatTime: 30 * 1000,
|
heartbeatTime : 30 * 1000,
|
||||||
// 心跳定时器
|
// 心跳定时器
|
||||||
heartbeatTimer: null,
|
heartbeatTimer: null,
|
||||||
// 是否重连
|
// 是否重连
|
||||||
reconnection: true,
|
reconnection : true,
|
||||||
|
// 防止重复重连
|
||||||
|
lockReconnect : false,
|
||||||
// 重连定时器
|
// 重连定时器
|
||||||
reconnectTimer: null,
|
reconnectTimer: null,
|
||||||
// 防止重复重连
|
|
||||||
lockReconnect: false,
|
|
||||||
// 当前重连时间
|
// 当前重连时间
|
||||||
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", {
|
||||||
battery: 100,
|
// TODO:电池信息
|
||||||
|
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
|
||||||
@@ -120,32 +129,32 @@ const signalChannel = {
|
|||||||
resolve(me.channel);
|
resolve(me.channel);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
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,
|
||||||
clientId: config.signal.clientId,
|
clientId : config.signal.clientId,
|
||||||
clientType: "MEDIA",
|
clientType: "MEDIA",
|
||||||
username: config.signal.username,
|
username : config.signal.username,
|
||||||
password: config.signal.password,
|
password : config.signal.password,
|
||||||
battery: 100,
|
// TODO:电池信息
|
||||||
charging: true,
|
battery : 100,
|
||||||
|
charging : true,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
me.reconnectionTimeout = me.minReconnectionDelay;
|
me.reconnectionTimeout = me.minReconnectionDelay;
|
||||||
me.taoyao.connect = true;
|
me.taoyao.connect = true;
|
||||||
me.heartbeat();
|
me.heartbeat();
|
||||||
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) {
|
||||||
|
const content = data.toString();
|
||||||
try {
|
try {
|
||||||
const content = data.toString();
|
console.debug("信令通道消息", content);
|
||||||
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
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 房间
|
* 房间
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ import {
|
|||||||
*/
|
*/
|
||||||
const protocol = {
|
const protocol = {
|
||||||
// 当前索引
|
// 当前索引
|
||||||
index: 0,
|
index : 0,
|
||||||
// 最大索引
|
// 最大索引
|
||||||
maxIndex: 999,
|
maxIndex : 999,
|
||||||
// 终端索引
|
// 终端索引
|
||||||
clientIndex: 99999,
|
clientIndex: 99999,
|
||||||
/**
|
/**
|
||||||
@@ -44,13 +44,14 @@ 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,
|
||||||
};
|
};
|
||||||
return message;
|
return message;
|
||||||
},
|
},
|
||||||
@@ -65,28 +66,28 @@ 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,
|
||||||
// 心跳定时器
|
// 心跳定时器
|
||||||
heartbeatTimer: null,
|
heartbeatTimer: null,
|
||||||
// 是否重连
|
// 是否重连
|
||||||
reconnection: true,
|
reconnection : true,
|
||||||
|
// 防止重复重连
|
||||||
|
lockReconnect : false,
|
||||||
// 重连定时器
|
// 重连定时器
|
||||||
reconnectTimer: null,
|
reconnectTimer: null,
|
||||||
// 防止重复重连
|
|
||||||
lockReconnect: false,
|
|
||||||
// 当前重连时间
|
// 当前重连时间
|
||||||
reconnectionTimeout: 5 * 1000,
|
reconnectionTimeout : 5 * 1000,
|
||||||
// 最小重连时间
|
// 最小重连时间
|
||||||
minReconnectionDelay: 5 * 1000,
|
minReconnectionDelay: 5 * 1000,
|
||||||
// 最大重连时间
|
// 最大重连时间
|
||||||
maxReconnectionDelay: 60 * 1000,
|
maxReconnectionDelay: 30 * 1000,
|
||||||
/**
|
/**
|
||||||
* 心跳
|
* 心跳
|
||||||
*/
|
*/
|
||||||
@@ -100,13 +101,13 @@ const signalChannel = {
|
|||||||
const battery = await navigator.getBattery();
|
const battery = await navigator.getBattery();
|
||||||
me.push(
|
me.push(
|
||||||
protocol.buildMessage("client::heartbeat", {
|
protocol.buildMessage("client::heartbeat", {
|
||||||
battery: battery.level * 100,
|
battery : battery.level * 100,
|
||||||
charging: battery.charging,
|
charging: battery.charging,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
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
|
||||||
@@ -132,32 +133,32 @@ const signalChannel = {
|
|||||||
resolve(me.channel);
|
resolve(me.channel);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
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", {
|
||||||
name: me.taoyao.name,
|
name : me.taoyao.name,
|
||||||
clientId: me.taoyao.clientId,
|
clientId : me.taoyao.clientId,
|
||||||
clientType: "WEB",
|
clientType: "WEB",
|
||||||
username: me.taoyao.username,
|
username : me.taoyao.username,
|
||||||
password: me.taoyao.password,
|
password : me.taoyao.password,
|
||||||
battery: battery.level * 100,
|
battery : battery.level * 100,
|
||||||
charging: battery.charging,
|
charging : battery.charging,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
me.reconnectionTimeout = me.minReconnectionDelay;
|
me.reconnectionTimeout = me.minReconnectionDelay;
|
||||||
me.taoyao.connect = true;
|
me.taoyao.connect = true;
|
||||||
me.heartbeat();
|
me.heartbeat();
|
||||||
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);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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
|
|
||||||
|
|||||||
Reference in New Issue
Block a user