PeerJS保存了一个飘带列表

时间:2017-02-21 19:16:15

标签: javascript node.js webrtc peerjs

我正在与PeerJS进行一个项目,其中一个同伴进入一个网页,开始观看一个电影片段,如果他愿意,它可以开始一个自己的网络摄像头和其他同伴的流访问同一个网页可以连接到该流,如果他们想要的话。

这启动一个流只不过是同伴按下按钮,他们的网络摄像头和麦克风都会显示给他们。只有当另一个对等体连接到对等流时,才实际上他正在流式传输。

现在我需要保存所有流式传输的对等体,因此,按下该按钮的对等体被放在服务器上的列表中,然后其他普通对等体(不是流式传输)可以请求该列表。

我在peer.js上做了这个:

    //Get a list with the IDs of peers that are streaming.
    Peer.prototype.announceStream = function(hash, cb) {

        cb = cb || function() {};
        var self = this;
        var http = new XMLHttpRequest();
        var protocol = this.options.secure ? 'https://' : 'http://';
        var url = protocol + this.options.host + ':' + this.options.port +
            this.options.path + this.options.key + '/streamer/' + this.id;
        console.log(url); //EMANUEL
        var queryString = '?ts=' + new Date().getTime() + '' + Math.random();
        url += queryString;

        // If there's no ID we need to wait for one before trying to init socket.
        http.open('get', url, true); //WHAT DO I PUT IN METHOD?
        http.onerror = function(e) {
            self._abort('server-error', 'Could not get peers from the server.');
            cb([]);
        };

        http.onreadystatechange = function() {
            if (http.readyState !== 4) {
                return;
            }
            if (http.status === 401) {
                var helpfulError = '';
                if (self.options.host !== util.CLOUD_HOST) {
                    helpfulError = 'It looks like you\'re using the cloud server. You can email ' +
                        'team@peerjs.com to enable peer listing for your API key.';
                } else {
                    helpfulError = 'You need to enable `allow_discovery` on your self-hosted ' +
                        'PeerServer to use this feature.';
                }
                cb([]);
                throw new Error('It doesn\'t look like you have permission to list peers IDs. ' + helpfulError);
            } else if (http.status !== 200) {
                cb([]);
            } else {
                cb(JSON.parse(http.responseText));
            }
        };

        http.send(null);

    };

这是关于index.js:

//Begins the stream of the user. Basically, gets its video and audio and displays to the user.
function startStream() {

    var constraints = { audio: true, video: true };

    navigator.getUserMedia(constraints, function(stream) {

        $('#myvideo').prop('src', URL.createObjectURL(stream));
        window.localStream = stream;
        document.getElementById('myVideoStreamHidden').style.display = "block";

        streamHash = btoa(localStream);
        streamHash = JSON.stringify(streamHash);
        peer.announceStream(streamHash);

    }, function(err) { console.log(err.name + ": " + err.message); });

};

请注意,只有在按下按钮后才会触发startStream函数。

但是当我按下"开始流"时,我收到404错误按钮。我的更新代码在这里:https://ufile.io/82c3e(index.js),https://ufile.io/efd91(peer.js)。

0 个答案:

没有答案