[^_^] 2024加油
This commit is contained in:
@@ -7,19 +7,20 @@ const WebSocket = require("ws");
|
||||
* 信令协议
|
||||
*/
|
||||
const protocol = {
|
||||
|
||||
// 当前索引
|
||||
index : 0,
|
||||
// 最大索引
|
||||
maxIndex : 999,
|
||||
// 终端索引
|
||||
clientIndex: 99999,
|
||||
|
||||
/**
|
||||
* @returns 索引
|
||||
*/
|
||||
buildId() {
|
||||
const me = this;
|
||||
if (++me.index > me.maxIndex) {
|
||||
me.index = 0;
|
||||
if (++this.index > this.maxIndex) {
|
||||
this.index = 0;
|
||||
}
|
||||
const date = new Date();
|
||||
return (
|
||||
@@ -27,10 +28,11 @@ const protocol = {
|
||||
1000000000000 * date.getHours() +
|
||||
10000000000 * date.getMinutes() +
|
||||
100000000 * date.getSeconds() +
|
||||
1000 * me.clientIndex +
|
||||
me.index
|
||||
1000 * this.clientIndex +
|
||||
this.index
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {*} signal 信令标识
|
||||
* @param {*} body 消息主体
|
||||
@@ -40,11 +42,10 @@ const protocol = {
|
||||
* @returns 信令消息
|
||||
*/
|
||||
buildMessage(signal, body = {}, id, v) {
|
||||
const me = this;
|
||||
const message = {
|
||||
header: {
|
||||
v : v || config.signal.version,
|
||||
id : id || me.buildId(),
|
||||
id : id || this.buildId(),
|
||||
signal: signal,
|
||||
},
|
||||
body: body,
|
||||
@@ -56,12 +57,13 @@ const protocol = {
|
||||
/**
|
||||
* 名称冲突
|
||||
*/
|
||||
const taoyaoProtocol = protocol;
|
||||
const taoyaoProtocol = protocol;
|
||||
|
||||
/**
|
||||
* 信令通道
|
||||
*/
|
||||
const signalChannel = {
|
||||
|
||||
// 桃夭信令
|
||||
taoyao : null,
|
||||
// 信令通道
|
||||
@@ -84,34 +86,35 @@ const signalChannel = {
|
||||
minReconnectionDelay: 5 * 1000,
|
||||
// 最大重连时间
|
||||
maxReconnectionDelay: 30 * 1000,
|
||||
|
||||
/**
|
||||
* 心跳
|
||||
*/
|
||||
heartbeat() {
|
||||
const me = this;
|
||||
if (me.heartbeatTimer) {
|
||||
clearTimeout(me.heartbeatTimer);
|
||||
if (this.heartbeatTimer) {
|
||||
clearTimeout(this.heartbeatTimer);
|
||||
}
|
||||
me.heartbeatTimer = setTimeout(async () => {
|
||||
if (me.connected()) {
|
||||
me.taoyao.push(protocol.buildMessage("client::heartbeat", {
|
||||
this.heartbeatTimer = setTimeout(async () => {
|
||||
if (this.connected()) {
|
||||
this.taoyao.push(protocol.buildMessage("client::heartbeat", {
|
||||
// TODO:电池信息
|
||||
battery : 100,
|
||||
charging: true,
|
||||
}));
|
||||
me.heartbeat();
|
||||
this.heartbeat();
|
||||
} else {
|
||||
console.warn("心跳失败", me.address);
|
||||
console.warn("心跳失败", this.address);
|
||||
}
|
||||
}, me.heartbeatTime);
|
||||
}, this.heartbeatTime);
|
||||
},
|
||||
|
||||
/**
|
||||
* @returns 是否连接成功
|
||||
*/
|
||||
connected() {
|
||||
const me = this;
|
||||
return me.channel && me.channel.readyState === WebSocket.OPEN;
|
||||
return this.channel && this.channel.readyState === WebSocket.OPEN;
|
||||
},
|
||||
|
||||
/**
|
||||
* 连接信令
|
||||
*
|
||||
@@ -121,20 +124,24 @@ const signalChannel = {
|
||||
* @returns Promise<WebSocket>
|
||||
*/
|
||||
async connect(address, reconnection = true) {
|
||||
const me = this;
|
||||
if (me.connected()) {
|
||||
if (this.connected()) {
|
||||
this.taoyao.connect = true;
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve(me.channel);
|
||||
resolve(this.channel);
|
||||
});
|
||||
} else {
|
||||
this.taoyao.connect = false;
|
||||
}
|
||||
me.address = address;
|
||||
me.reconnection = reconnection;
|
||||
this.address = address;
|
||||
this.reconnection = reconnection;
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug("连接信令通道", me.address);
|
||||
me.channel = new WebSocket(me.address, { rejectUnauthorized: false, handshakeTimeout: 5000 });
|
||||
me.channel.on("open", async () => {
|
||||
console.info("打开信令通道", me.address);
|
||||
const { body } = await me.taoyao.request(protocol.buildMessage("client::register", {
|
||||
console.debug("连接信令通道", this.address);
|
||||
this.channel = new WebSocket(this.address, { rejectUnauthorized: false, handshakeTimeout: 5000 });
|
||||
this.channel.on("open", async () => {
|
||||
console.debug("打开信令通道", this.address);
|
||||
const {
|
||||
body
|
||||
} = await this.taoyao.request(protocol.buildMessage("client::register", {
|
||||
name : config.signal.name,
|
||||
clientId : config.signal.clientId,
|
||||
clientType: config.signal.clientType,
|
||||
@@ -144,84 +151,81 @@ const signalChannel = {
|
||||
battery : 100,
|
||||
charging : true,
|
||||
}));
|
||||
protocol.clientIndex = body.index;
|
||||
console.info("终端注册成功", protocol.clientIndex);
|
||||
me.reconnectionTimeout = me.minReconnectionDelay;
|
||||
me.taoyao.connect = true;
|
||||
me.heartbeat();
|
||||
resolve(me.channel);
|
||||
protocol.clientIndex = body.index;
|
||||
this.taoyao.connect = true;
|
||||
this.reconnectionTimeout = this.minReconnectionDelay;
|
||||
console.debug("终端注册成功", protocol.clientIndex);
|
||||
this.heartbeat();
|
||||
resolve(this.channel);
|
||||
});
|
||||
me.channel.on("close", async () => {
|
||||
console.warn("信令通道关闭", me.address);
|
||||
me.taoyao.connect = false;
|
||||
if(!me.connected()) {
|
||||
me.taoyao.closeAllRoom();
|
||||
}
|
||||
if (me.reconnection) {
|
||||
me.reconnect();
|
||||
this.channel.on("close", async () => {
|
||||
console.warn("信令通道关闭", this.address);
|
||||
this.taoyao.connect = false;
|
||||
this.taoyao.closeAllRoom();
|
||||
if (this.reconnection) {
|
||||
this.reconnect();
|
||||
}
|
||||
// 不要失败回调
|
||||
});
|
||||
me.channel.on("error", async (e) => {
|
||||
console.error("信令通道异常", me.address, e);
|
||||
this.channel.on("error", async (e) => {
|
||||
console.error("信令通道异常", this.address, e);
|
||||
// 不要失败回调
|
||||
});
|
||||
me.channel.on("message", async (data) => {
|
||||
this.channel.on("message", async (data) => {
|
||||
const content = data.toString();
|
||||
try {
|
||||
console.debug("信令通道消息", content);
|
||||
me.taoyao.on(JSON.parse(content));
|
||||
this.taoyao.on(JSON.parse(content));
|
||||
} catch (error) {
|
||||
console.error("处理信令通道消息异常", content, error);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 重连信令
|
||||
*/
|
||||
reconnect() {
|
||||
const me = this;
|
||||
if (
|
||||
me.lockReconnect ||
|
||||
me.taoyao.connect ||
|
||||
me.connected()
|
||||
) {
|
||||
if (this.connected() || this.lockReconnect) {
|
||||
return;
|
||||
}
|
||||
me.lockReconnect = true;
|
||||
if (me.reconnectTimer) {
|
||||
clearTimeout(me.reconnectTimer);
|
||||
this.lockReconnect = true;
|
||||
if (this.reconnectTimer) {
|
||||
clearTimeout(this.reconnectTimer);
|
||||
}
|
||||
// 定时重连
|
||||
me.reconnectTimer = setTimeout(() => {
|
||||
console.info("重连信令通道", me.address);
|
||||
me.connect(me.address, me.reconnection);
|
||||
me.lockReconnect = false;
|
||||
}, me.reconnectionTimeout);
|
||||
me.reconnectionTimeout = Math.min(
|
||||
me.reconnectionTimeout + me.minReconnectionDelay,
|
||||
me.maxReconnectionDelay
|
||||
this.reconnectTimer = setTimeout(() => {
|
||||
console.debug("重连信令通道", this.address);
|
||||
this.connect(this.address, this.reconnection);
|
||||
this.lockReconnect = false;
|
||||
}, this.reconnectionTimeout);
|
||||
// 设置重连时间
|
||||
this.reconnectionTimeout = Math.min(
|
||||
this.reconnectionTimeout + this.minReconnectionDelay,
|
||||
this.maxReconnectionDelay
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* 关闭通道
|
||||
*/
|
||||
close() {
|
||||
const me = this;
|
||||
console.info("关闭信令通道", me.address);
|
||||
clearTimeout(me.heartbeatTimer);
|
||||
clearTimeout(me.reconnectTimer);
|
||||
me.reconnection = false;
|
||||
me.taoyao.connect = false;
|
||||
me.channel.close();
|
||||
console.debug("关闭信令通道", this.address);
|
||||
clearTimeout(this.heartbeatTimer);
|
||||
clearTimeout(this.reconnectTimer);
|
||||
this.reconnection = false;
|
||||
this.taoyao.connect = false;
|
||||
this.channel.close();
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* 房间
|
||||
*/
|
||||
class Room {
|
||||
|
||||
// 是否关闭
|
||||
close = null;
|
||||
// 房间ID
|
||||
|
||||
Reference in New Issue
Block a user