javascript - UV4L Restful API 轮询事件
问题描述
我正在使用 UV4L 的 restful API 来配置 Janus 客户端。有些事件是异步的,需要您轮询事件端点以获取响应,例如加入请求。
我的问题是,在发送加入请求时,我永远不会收到它的事件。我只在轮询事件端点时才得到这个:
/api/janus/client/events?maxev=0&timeout_s=5
{ what: 'keepalive' }
UV4L API 中没有记录此响应,因此我不确定这意味着什么。我确保我的配置已经为 UV4L 服务器启用了 keepalive(默认情况下它是打开的)。我将查询设置为 5 秒的超时并将 maxev 设置为 0,因此它应该获取所有事件。我的获取请求也立即返回,而不是保持活动 5 秒,这看起来很奇怪。使用内置的 UV4L restful api 面板时也会发生同样的事情。
在进行加入请求之前,设置和会话端点都已被调用并成功返回。获取房间参与者列表表明加入成功,我只是从未看到该事件。我需要事件知道何时可以安全地发布到房间。
这是我调用 Restful API 的代码,但因为 UV4L 面板做同样的事情,我不认为这是我的代码的问题:
class UV4L {
static async joinRoom(info) {
try {
await fs.promises.access('/dev/video0');
} catch(err) {
throw new Error('No video camera detected');
}
const janusConfig = require('../janus.json');
janusConfig.gateway.url = info.gateway.url;
janusConfig.gateway.root = info.gateway.root;
// Stop previous JANUS session
console.log('Destroying previous session');
const destroy = await UV4L.fetch(URL + '/api/janus/client', 'POST', {
"what": "destroy",
"plugin": "videoroom",
});
// Send the JANUS settings
console.log('Updating Janus settings');
const settings = await UV4L.fetch(URL + '/api/janus/client/settings', 'PUT', janusConfig);
if(settings.response.code !== 200) {
throw new Error(settings.response.reason);
}
// Create a new JANUS session
console.log('Creating new session');
const session = await UV4L.fetch(URL + '/api/janus/client', 'POST', {
"what": "create",
"plugin": "videoroom",
});
if(session.error) {
throw new Error(session.error.reason);
}
// Join the JANUS room
console.log('Joining JANUS room');
const joinRoom = await UV4L.fetch(URL + '/api/janus/client/videoroom', 'POST', {
"what": "join",
"body": {
room: info.room,
room_pin: info.pin,
username: os.hostname,
}
});
console.log('Publishing stream to JANUS');
}
static async fetch(url, method, data) {
// Generate transaction if it doesnt already exist
if(data && !data.transaction) {
data.transaction = uuidv4();
}
const options = {
method,
body: JSON.stringify(data)
};
// Enable keep alive if get
if(method.toLowerCase() === 'get') {
options.keepalive = true;
}
// Fetch from the API
let response = await await fetch(url, options);
// Convert response to JSON
response = await response.json();
// If the response is an ack, wait for its event
if(data && response.what === 'ack') {
response = await this.waitForAck(data.transaction);
}
return response;
}
static async waitForAck(transaction) {
return new Promise(async (resolve, reject) => {
// Poll event API until transaction found
while(true) {
const response = await this.fetch(URL + '/api/janus/client/events?maxev=0&timeout_s=5', 'get');
if(response.transaction === transaction) {
return response;
}
}
});
}
}
解决方案
推荐阅读
- flutter - 如何手动将 .xcframework 添加到 Flutter iOS 插件?
- c++ - 从基类访问静态数组时出现段错误
- sql - Sql 基于多行的选择,如 GraphQL
- javascript - 为什么它以这种方式解释代码?
- linux - 将目录挂载到容器后,容器中目录中的文件大小变为0
- angularjs - 使用 AngularJS 显示“个人或组”列内容
- javascript - React Hook - 具有纯函数的 setData 以使用 prevData 但内部进行了深入比较
- javascript - 为什么当我关闭并重新打开反应组件(材料表)时会发生内存泄漏并渲染速度变慢?
- python - Scipy wavfile 在 AWS Lambda 上采样 [时间限制错误]。为什么?
- google-sheets - 当 B 列为空时计算 A 列中的值,反之亦然