【发布时间】:2018-08-21 11:09:06
【问题描述】:
我正在将 twilio 视频实现到我的 js 应用程序中。我想要的行为是,如果远程参与者与房间断开连接,本地参与者也应该离开房间并且网络摄像头应该停用。
但是发生的情况是参与者离开并且网络摄像头 LED 仍亮着,并且它似乎仍在流式传输到 twilio,直到我关闭浏览器。
我将如何解决这个问题?
function connectVideo(accessToken){
const Video = Twilio.Video;
Video.connect(accessToken, { name: customerToken}).then(room => {
console.log('Connected to Room "%s"', room.name);
inCall = 1
room.participants.forEach(participantConnected);
room.on('participantConnected', participantConnected);
room.once('participantDisconnected', participant => {
console.log(`Participant "${participant.identity}" has disconnected from the Room!`);
room.disconnect();
var div = document.getElementById(participant.sid);
div.remove()
inCall = 0
});
room.on('disconnected', room => {
// Detach the local media elements
room.localParticipant.tracks.forEach(publication => {
const attachedElements = publication.track.detach();
console.log("unsubscribed from: " + publication.track)
attachedElements.forEach(element => element.remove());
});
});
});
function participantConnected(participant) {
console.log('Participant "%s" connected', participant.identity);
const div = document.createElement('div');
div.id = participant.sid;
participant.on('trackSubscribed', track => trackSubscribed(div, track));
participant.on('trackUnsubscribed', trackUnsubscribed);
participant.tracks.forEach(publication => {
if (publication.isSubscribed) {
trackSubscribed(div, publication.track);
}
});
document.body.appendChild(div);
}
function trackSubscribed(div, track) {
div.appendChild(track.attach());
}
function trackUnsubscribed(track) {
track.detach().forEach(element => element.remove());
}
}
【问题讨论】:
标签: javascript twilio