Tokbox视频聊天查询

Tokbox video chat query

本文关键字:查询 视频聊天 Tokbox      更新时间:2023-09-26

我正在使用Tokbox视频聊天开发网站,如果两个用户都已连接,并且他们的视频都可以访问,我必须运行计时器。

我使用下面的代码,如果有用户连接,它会调用start_countdown函数。

 session.on('streamCreated', function(event) {
    event.streams.forEach(function(stream) {
      if(stream.connection.connectionId != session.connection.connectionId) {
        // subscribe(stream);
        console.log("New stream in the session: " + event);
        // console.log(event);
        // console.log(stream);
        // start_countdown();
      }
    });
  });

但如果两个用户都允许访问他们的视频,我需要调用倒计时功能。有人能帮上忙吗。

首先要理解的是,由于您在客户端代码中这样做,每个参与者都有自己的倒计时值,因为两个浏览器都独立运行计时器。如果您只是想在UI中显示两个用户已经连接(或剩余)的时间,那么这可能没问题。但如果你试图保持一个真正的时钟,这种方法可能不是最好的。

接下来,在OpenTok中,当远程流可用时,会话会触发streamCreated事件。当本地流开始发送时(在本地用户授予访问权限之后),发布服务器还会触发streamCreated事件。如果您知道会话中只有两个用户,则可以通过监听这两个事件来了解两个用户何时加入。以下是一个示例:

// Initialize flags
var publishing = false;
var subscribing = false;
// TODO: create a publisher (either using session.publish() or OT.initPublisher()) and publish a stream after the session has connected
// Listen to the events
session.on('streamCreated', function(event) {
  // event.stream will always be a remote stream so the connection comparison is unnecessary
  // TODO: subscribe to the stream
  subscribing = true;
  // Check if both criteria have been met
  checkStartCountdown();
});
publisher.on('streamCreated', function(event) {
  publishing = true;
  // Check if both criteria have been met
  checkStartCountdown();
});
function checkStartCountdown() {
  if (publishing && subscribing) {
    // TODO: start_countdown is the function as you described in your question
    start_countdown();
  }
}