[*] 日常优化
This commit is contained in:
@@ -363,20 +363,17 @@ class Room {
|
|||||||
me.transports.forEach(v => v.close());
|
me.transports.forEach(v => v.close());
|
||||||
me.mediasoupRouter.close();
|
me.mediasoupRouter.close();
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// TODO:continue
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 桃夭
|
* 桃夭信令
|
||||||
*/
|
*/
|
||||||
class Taoyao {
|
class Taoyao {
|
||||||
// 是否连接
|
// 是否连接
|
||||||
connect = false;
|
connect = false;
|
||||||
// 房间列表
|
// 房间列表:房间ID=房间
|
||||||
rooms = new Map();
|
rooms = new Map();
|
||||||
// 回调事件
|
// 回调事件:消息ID=事件
|
||||||
callbackMapping = new Map();
|
callbackMapping = new Map();
|
||||||
// Worker列表
|
// Worker列表
|
||||||
mediasoupWorkers = [];
|
mediasoupWorkers = [];
|
||||||
@@ -384,6 +381,7 @@ class Taoyao {
|
|||||||
nextMediasoupWorkerIndex = 0;
|
nextMediasoupWorkerIndex = 0;
|
||||||
|
|
||||||
constructor(mediasoupWorkers) {
|
constructor(mediasoupWorkers) {
|
||||||
|
console.info("加载媒体桃夭信令");
|
||||||
this.mediasoupWorkers = mediasoupWorkers;
|
this.mediasoupWorkers = mediasoupWorkers;
|
||||||
// 定时打印使用情况
|
// 定时打印使用情况
|
||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
@@ -398,29 +396,31 @@ class Taoyao {
|
|||||||
*/
|
*/
|
||||||
on(message) {
|
on(message) {
|
||||||
const me = this;
|
const me = this;
|
||||||
|
// 解构
|
||||||
|
const { header, body } = message;
|
||||||
|
const { id, signal } = header;
|
||||||
// 请求回调
|
// 请求回调
|
||||||
if (me.callbackMapping.has(message.header.id)) {
|
if (me.callbackMapping.has(id)) {
|
||||||
try {
|
try {
|
||||||
me.callbackMapping.get(message.header.id)(message);
|
me.callbackMapping.get(id)(message);
|
||||||
} finally {
|
} finally {
|
||||||
me.callbackMapping.delete(message.header.id);
|
me.callbackMapping.delete(id);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 执行信令
|
// 执行信令
|
||||||
const body = message.body;
|
switch (signal) {
|
||||||
switch (message.header.signal) {
|
|
||||||
case "client::reboot":
|
case "client::reboot":
|
||||||
me.clientReboot(message, body);
|
me.clientReboot(message, body);
|
||||||
break;
|
break;
|
||||||
|
case "client::register":
|
||||||
|
me.clientRegister(message, body);
|
||||||
|
break;
|
||||||
case "client::shutdown":
|
case "client::shutdown":
|
||||||
me.clientShutdown(message, body);
|
me.clientShutdown(message, body);
|
||||||
break;
|
break;
|
||||||
case "client::register":
|
case "control::server::record":
|
||||||
protocol.clientIndex = body.index;
|
me.controlServerRecord(message, body);
|
||||||
break;
|
|
||||||
case "media::ice::restart":
|
|
||||||
me.mediaIceRestart(message, body);
|
|
||||||
break;
|
break;
|
||||||
case "media::consume":
|
case "media::consume":
|
||||||
me.mediaConsume(message, body);
|
me.mediaConsume(message, body);
|
||||||
@@ -467,9 +467,6 @@ class Taoyao {
|
|||||||
case "media::producer::resume":
|
case "media::producer::resume":
|
||||||
me.mediaProducerResume(message, body);
|
me.mediaProducerResume(message, body);
|
||||||
break;
|
break;
|
||||||
case "control::server::record":
|
|
||||||
me.controlServerRecord(message, body);
|
|
||||||
break;
|
|
||||||
case "media::router::rtp::capabilities":
|
case "media::router::rtp::capabilities":
|
||||||
me.mediaRouterRtpCapabilities(message, body);
|
me.mediaRouterRtpCapabilities(message, body);
|
||||||
break;
|
break;
|
||||||
@@ -506,7 +503,7 @@ class Taoyao {
|
|||||||
try {
|
try {
|
||||||
signalChannel.channel.send(JSON.stringify(message));
|
signalChannel.channel.send(JSON.stringify(message));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("异步请求异常:", message, error);
|
console.error("异步请求异常", message, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -520,11 +517,18 @@ class Taoyao {
|
|||||||
async request(message) {
|
async request(message) {
|
||||||
const me = this;
|
const me = this;
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let done = false;
|
const { header, body } = message;
|
||||||
// 注册回调
|
const { id } = header;
|
||||||
me.callbackMapping.set(message.header.id, (response) => {
|
// 设置超时
|
||||||
|
const rejectTimeout = setTimeout(() => {
|
||||||
|
me.callbackMapping.delete(id);
|
||||||
|
reject("请求超时", message);
|
||||||
|
}, 5000);
|
||||||
|
// 请求回调
|
||||||
|
me.callbackMapping.set(id, (response) => {
|
||||||
resolve(response);
|
resolve(response);
|
||||||
done = true;
|
clearTimeout(rejectTimeout);
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
// 发送消息
|
// 发送消息
|
||||||
try {
|
try {
|
||||||
@@ -532,16 +536,11 @@ class Taoyao {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject("同步请求异常", error);
|
reject("同步请求异常", error);
|
||||||
}
|
}
|
||||||
// 设置超时
|
|
||||||
setTimeout(() => {
|
|
||||||
if (!done) {
|
|
||||||
me.callbackMapping.delete(message.header.id);
|
|
||||||
reject("请求超时", message);
|
|
||||||
}
|
|
||||||
}, 5000);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO:continue
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打印日志
|
* 打印日志
|
||||||
*/
|
*/
|
||||||
@@ -590,6 +589,17 @@ class Taoyao {
|
|||||||
// this.push(message);
|
// this.push(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 终端注册信令
|
||||||
|
*
|
||||||
|
* @param {*} message 消息
|
||||||
|
* @param {*} body 消息主体
|
||||||
|
*/
|
||||||
|
clientRegister(message, body) {
|
||||||
|
protocol.clientIndex = body.index;
|
||||||
|
console.debug("终端序号", protocol.clientIndex);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 关闭终端信令
|
* 关闭终端信令
|
||||||
*
|
*
|
||||||
@@ -1766,6 +1776,6 @@ class Taoyao {
|
|||||||
// console.info("mediasoupRouter newrtpobserver:", roomId, mediasoupRouter.id, rtpObserver.id);
|
// console.info("mediasoupRouter newrtpobserver:", roomId, mediasoupRouter.id, rtpObserver.id);
|
||||||
// });
|
// });
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = { Taoyao, signalChannel };
|
module.exports = { Taoyao, signalChannel };
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ export default {
|
|||||||
this.audioStream.addTrack(track);
|
this.audioStream.addTrack(track);
|
||||||
this.audio.srcObject = this.audioStream;
|
this.audio.srcObject = this.audioStream;
|
||||||
}
|
}
|
||||||
this.audio.play().catch((error) => console.warn("视频播放失败", error));
|
this.audio.play().catch((error) => console.warn("音频播放失败", error));
|
||||||
} else if(track.kind === 'video') {
|
} else if(track.kind === 'video') {
|
||||||
this.videoConsumer = consumer;
|
this.videoConsumer = consumer;
|
||||||
if (this.videoStream) {
|
if (this.videoStream) {
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ export default {
|
|||||||
this.audioStream.addTrack(track);
|
this.audioStream.addTrack(track);
|
||||||
this.audio.srcObject = this.audioStream;
|
this.audio.srcObject = this.audioStream;
|
||||||
}
|
}
|
||||||
this.audio.play().catch((error) => console.warn("视频播放失败", error));
|
this.audio.play().catch((error) => console.warn("音频播放失败", error));
|
||||||
} else if(track.kind === 'video') {
|
} else if(track.kind === 'video') {
|
||||||
if (this.videoStream) {
|
if (this.videoStream) {
|
||||||
// TODO:资源释放
|
// TODO:资源释放
|
||||||
|
|||||||
@@ -601,11 +601,18 @@ class Taoyao extends RemoteClient {
|
|||||||
async request(message) {
|
async request(message) {
|
||||||
const me = this;
|
const me = this;
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let done = false;
|
const { header, body } = message;
|
||||||
|
const { id } = header;
|
||||||
|
// 设置超时
|
||||||
|
const rejectTimeout = setTimeout(() => {
|
||||||
|
me.callbackMapping.delete(id);
|
||||||
|
reject("请求超时", message);
|
||||||
|
}, 5000);
|
||||||
// 请求回调
|
// 请求回调
|
||||||
me.callbackMapping.set(message.header.id, (response) => {
|
me.callbackMapping.set(id, (response) => {
|
||||||
resolve(response);
|
resolve(response);
|
||||||
done = true;
|
clearTimeout(rejectTimeout);
|
||||||
|
// 默认不用继续处理
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
// 发送消息
|
// 发送消息
|
||||||
@@ -614,13 +621,6 @@ class Taoyao extends RemoteClient {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject("同步请求异常", error);
|
reject("同步请求异常", error);
|
||||||
}
|
}
|
||||||
// 设置超时
|
|
||||||
setTimeout(() => {
|
|
||||||
if (!done) {
|
|
||||||
me.callbackMapping.delete(message.header.id);
|
|
||||||
reject("请求超时", message);
|
|
||||||
}
|
|
||||||
}, 5000);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/************************ 回调 ************************/
|
/************************ 回调 ************************/
|
||||||
@@ -939,8 +939,8 @@ class Taoyao extends RemoteClient {
|
|||||||
* @param {*} message 消息
|
* @param {*} message 消息
|
||||||
*/
|
*/
|
||||||
defaultClientRegister(message) {
|
defaultClientRegister(message) {
|
||||||
const { index } = message.body;
|
protocol.clientIndex = message.body.index;
|
||||||
protocol.clientIndex = index;
|
console.debug("终端序号", protocol.clientIndex);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 关闭终端信令
|
* 关闭终端信令
|
||||||
|
|||||||
Reference in New Issue
Block a user