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