加入库伦托的多个房间

时间:2018-08-29 03:05:19

标签: javascript kurento video-conferencing

我想创建一个允许用户加入kurento中多个视频呼叫室的应用程序。
我写了以下代码:

function joinRooms() {

    var userId = document.getElementById('expertid').value;
    console.log("UserID = " + userId);
    var roomIds = document.getElementById('roomids').value.split(",");//Assume that we have 2 rooms r1 & r2
    var userIdAlias = [];
    for (var i = 0; i < roomIds.length; i++) {
        userIdAlias[i] = userId + "(" + i + ")";
        console.log("User alias = " + userIdAlias[i]);
    }

    var wsUri = 'wss://' + location.host + '/room';

    kurento = KurentoRoom(wsUri, function(error, kurento) {


        if (error)
            return console.log(error);
        for (let i = 0; i < roomIds.length; i++) {
            //console.log("Start loop Roomid = " + roomId);
            rooms[i] = kurento.Room({
                room : roomIds[i],
                user : userIdAlias[i],
                subscribeToStreams : true
            });
            localStreams[i] = kurento.Stream(rooms[i], {
                audio : true,
                video : true,
                data : true
            });
            localStreams[i].addEventListener("access-accepted", function() {

                var playVideo = function(stream) {
                    var participantId = stream.getParticipant().getID();
                    var userId = document.getElementById('expertid').value;
                    console.log("userId = " + userId + ", participantId = " + participantId);
                    if (!userIdAlias.includes(participantId)) {
                        var elementId = "video-" + stream.getGlobalID();
                        var div = document.createElement('div');
                        div.setAttribute("id", elementId);
                        document.getElementById("participants").appendChild(div);
                        stream.playThumbnail(elementId);

                        // Check color
                        var videoTag = document.getElementById("native-" + elementId);
                        var userId = stream.getGlobalID();
                        var canvas = document.createElement('CANVAS');
                        checkColor(videoTag, canvas, userId);
                    }

                }

                rooms[i].addEventListener("room-connected", function(){
                    var j = i;
                    return function(roomEvent) {

                        document.getElementById('room-header').innerText = 'ROOM \"'
                                + roomIds[j] + '\"';
                        //document.getElementById('join').style.display = 'none';
                        document.getElementById('room').style.display = 'block';

                        localStreams[j].publish();

                        var streams = roomEvent.streams;
                        for (var streamsIdx = 0; streamsIdx < streams.length; streamsIdx++) {
                            playVideo(streams[streamsIdx]);
                        }
                    }
                });

                rooms[i].addEventListener("stream-added", function(streamEvent) {
                    playVideo(streamEvent.stream);
                });

                rooms[i].addEventListener("stream-removed", function(streamEvent) {
                    var element = document.getElementById("video-"
                            + streamEvent.stream.getGlobalID());
                    if (element !== undefined) {
                        element.parentNode.removeChild(element);
                    }
                });

                playVideo(localStreams[i]);

                rooms[i].connect();
            });

            localStreams[i].init();
        }
    });
}

但是,该代码不起作用。登录服务器说只有一个用户请求加入房间r2,但没有加入r1。
我认为原因是Javascript可处理太多异步任务,因此无法正常工作。 您可以提供一些有关如何在kurento中加入多个房间的建议吗?

0 个答案:

没有答案