@webgrig

Видео-чат WebRtc. Как получить список всех комнат?

Сделал видео чат на основе скрипта simpleWebrtc simplewebrtc.com, есть возможность создавать отдельные комнаты для общения, количество участников в каждой комнате может быть любое.

Но я столкнулся с проблемой, мне нужно предоставить возможность участникам выбрать комнату в которой он хочет общаться, для этого нужно вывести на страницу ссылки всех комнат, и эти ссылки должны обновляться в режиме реального времени.

Мой вопрос как получить список всех открытых комнат?
  • Вопрос задан
  • 896 просмотров
Пригласить эксперта
Ответы на вопрос 2
mourr
@mourr
Passionate JS developer
Если вы используете свой сигнальный сервер (Signalmaster) - в sockets.js можно легко допилить получение комнат, т.к. он работает на socket.io

function findRooms() {
    var availableRooms = [];
    var rooms = io.sockets.adapter.rooms;
    if (rooms) {
        for (var room in rooms) {
            if (!rooms[room].hasOwnProperty(room)) {
                availableRooms.push(room);
            }
        }
    }
    return availableRooms;
}
Ответ написан
Комментировать
@webgrig Автор вопроса
При попытке обратиться к объекту io, мне выдает ошибку, и говорит, что такого свойства не существует.
Дело в том, что я использую скрипт simpleWebrtc, а у него почему то не создается экземпляр io, поэтому мне нужно получить тоже что написали вы но используя API simpleWebrtc. Вот сам скрипт:

// grab the room from the URL
            var room = location.search && location.search.split('?')[1];

            // create our webrtc connection
            var webrtc = new SimpleWebRTC({
                // the id/element dom element that will hold "our" video
                localVideoEl: 'localVideo',
                // the id/element dom element that will hold remote videos
                remoteVideosEl: '',
                // immediately ask for camera access
                autoRequestMedia: true,
                debug: false,
                detectSpeakingEvents: true,
                autoAdjustMic: false
            });

            // when it's ready, join if we got a room from the URL
            webrtc.on('readyToCall', function () {
                // you can name it anything
                if (room) webrtc.joinRoom(room);
            });

            function showVolume(el, volume) {
                if (!el) return;
                if (volume < -45) { // vary between -45 and -20
                    el.style.height = '0px';
                } else if (volume > -20) {
                    el.style.height = '100%';
                } else {
                    el.style.height = '' + Math.floor((volume + 100) * 100 / 25 - 220) + '%';
                }
            }
            webrtc.on('channelMessage', function (peer, label, data) {
                if (data.type == 'volume') {
                    showVolume(document.getElementById('volume_' + peer.id), data.volume);
                }
            });
            webrtc.on('videoAdded', function (video, peer) {
                console.log('video added', peer);
                var remotes = document.getElementById('remotes');
                if (remotes) {
                    var d = document.createElement('div');
                    d.className = 'videoContainer';
                    d.id = 'container_' + webrtc.getDomId(peer);
                    d.appendChild(video);
                    var vol = document.createElement('div');
                    vol.id = 'volume_' + peer.id;
                    vol.className = 'volume_bar';
                    video.onclick = function () {
                        console.log(video.style.width);
                        console.log(video.style.height);
                        video.style.width = video.videoWidth + 'px';
                        video.style.height = video.videoHeight + 'px';
                        console.log(video.style.width);
                        console.log(video.style.height);
                    };
                    d.appendChild(vol);
                    remotes.appendChild(d);
                }
            });
            webrtc.on('videoRemoved', function (video, peer) {
                console.log('video removed ', peer);
                var remotes = document.getElementById('remotes');
                var el = document.getElementById('container_' + webrtc.getDomId(peer));
                if (remotes && el) {
                    remotes.removeChild(el);
                }
            });
            webrtc.on('volumeChange', function (volume, treshold) {
                //console.log('own volume', volume);
                showVolume(document.getElementById('localVolume'), volume);
            });

            // Since we use this twice we put it here
            function setRoom(name) {
                //$('form').remove();
                //$('h1').text(name);
                //$('#subTitle').text('Link to join: ' + location.href);
                $('body').addClass('active');
            }
            if (room) {
                setRoom(room);
            } else {
                var val;
                webrtc.createRoom(val, function (err, name) {
                    console.log(' create room cb', arguments);

                    var newUrl = location.pathname + '?' + name;
                    if (!err) {
                        history.replaceState({foo: 'bar'}, null, newUrl);
                        setRoom(name);
                    } else {
                        console.log(err);
                    }
                });
            }

            var button = $('#screenShareButton'),
                setButton = function (bool) {
                    button.text(bool ? 'share screen' : 'stop sharing');
                };
            webrtc.on('localScreenStopped', function () {
                setButton(true);
            });

            setButton(true);

            button.click(function () {
                if (webrtc.getLocalScreen()) {
                    webrtc.stopScreenShare();
                    setButton(true);
                } else {
                    webrtc.shareScreen(function (err) {
                        if (err) {
                            setButton(true);
                        } else {
                            setButton(false);
                        }
                    });

                }
            });
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы