要理解的第一个想法是,由于您在客户端代码中执行此操作,因此每个参与者都有自己的倒计时值,因为两个浏览器都独立运行计时器。如果您只是想在 UI 中显示两个用户已连接(或剩余)的时间,这可能没问题。但是,如果您试图保持一个真正的时钟,那么这种方法可能不是最好的。
接下来,在 OpenTok 中,streamCreated
当远程流可用时,会从 Session 触发一个事件。streamCreated
当本地流开始发送时(在本地用户授予访问权限之后),发布者也会触发一个事件。如果您知道会话中只有 2 个用户,您可以通过监听这两个事件来了解两个用户何时加入。下面是一个例子:
// 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();
}
}