WebRTC和Socket.io(不发送消息)

时间:2017-10-17 11:55:16

标签: node.js sockets socket.io webrtc

我有以下代码,我的问题是socket.io没有发送消息......有什么想法吗?

我已尝试过.emit('广播','测试')但没有成功。获取用户媒体后,我发送了一条消息用于测试目的,打开两个使用相同代码的选项卡,没有一个选项卡收到消息。

我已经阅读了一些帖子,告诉我应该广播消息,而不是通过当前套接字发送。但是我没有为此目的使用io.sockets.send()方法。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebRTC Test</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
    <script>
        var localVideo;
        var remoteVideo;
        var peerConnection;
        var uuid;
        var serverConnection;
        var localStream;

        var peerConnectionConfig = {
            'iceServers': [
                {'urls': 'stun:stun.services.mozilla.com'},
                {'urls': 'stun:stun.l.google.com:19302'},
            ]
        };

        window.addEventListener('load', pageReady);

        function pageReady() {
            uuid = uuid();

            localVideo = document.getElementById('localVideo');
            remoteVideo = document.getElementById('remoteVideo');

            serverConnection = io.connect('https://rtcmulticonnection.herokuapp.com:443');

            serverConnection.on('message', gotMessageFromServer);

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

            if(navigator.mediaDevices.getUserMedia) {
                navigator.mediaDevices.getUserMedia(constraints).then(getUserMediaSuccess).catch(errorHandler);
            } else {
                alert('Your browser does not support getUserMedia API');
            }
        }

        function getUserMediaSuccess(stream) {
            localStream = stream;
            localVideo.srcObject = stream;
            serverConnection.send('test');
        }

        function start(isCaller) {
            peerConnection = new RTCPeerConnection(peerConnectionConfig);
            peerConnection.onicecandidate = gotIceCandidate;
            peerConnection.onaddstream = gotRemoteStream;
            peerConnection.addStream(localStream);

            if(isCaller) {
                peerConnection.createOffer().then(createdDescription).catch(errorHandler);
            }
        }

        function gotMessageFromServer(message) {
            console.log('Got message', message);
            if(!peerConnection) start(false);

            var signal = JSON.parse(message.data);

            // Ignore messages from ourself
            if(signal.uuid === uuid) return;

            if(signal.sdp) {
                peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
                    // Only create answers in response to offers
                    if(signal.sdp.type === 'offer') {
                        peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
                    }
                }).catch(errorHandler);
            } else if(signal.ice) {
                peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
            }
        }

        function gotIceCandidate(event) {
            if(event.candidate !== null) {
                serverConnection.send({'ice': event.candidate, 'uuid': uuid});
            }
        }

        function createdDescription(description) {
            console.log('got description');

            peerConnection.setLocalDescription(description).then(function() {
                console.log("Sending SDP:", description);
                serverConnection.emit('sdp', {'sdp': peerConnection.localDescription, 'uuid': uuid});
            }).catch(errorHandler);
        }

        function gotRemoteStream(event) {
            console.log('got remote stream');
            remoteVideo.srcObject = event.stream;
        }

        function errorHandler(error) {
            console.log(error);
        }

        function uuid() {
            function s4() {
                return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
            }

            return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
        }

    </script>
</head>
<body>
    Service is up

    <video id="localVideo" autoplay></video>
    <a href="javascript:start(true)">Start call</a>
    <video id="remoteVideo"></video>
</body>
</html>

0 个答案:

没有答案