将STUN服务器集成到XSockets.NET WebRTC中

时间:2014-10-22 13:57:31

标签: webrtc xsockets.net

我对此很陌生。 我如何将STUN功能添加到现有的XSockets.NET WebRTC Javascript代码中?

这个JS创建了我的连接。我在运行mono的linux机器上独立运行信令服务器。

<script type="text/javascript">
    $(document).ready(function() 
    {
        var rtc, ws, currentContext, change;
        var roomName = '<?php echo $roomName; ?>';
        var myCtx = '<?php echo $roomHash; ?>'; 
        var XSocketsServerIP = '<?php echo $XSocketsServerIP; ?>';              
        var startofurl = "/";
        var myCtxURLVersion = startofurl.concat(roomName);

        window.history.pushState("", "title", myCtxURLVersion);

        //Create new XSockets WebSocket.
        ws = new XSockets.WebSocket(XSocketsServerIP);
        ws.onopen = function (connection) 
        {
            console.log("Connection", connection);          

            rtc = new XSockets.WebRTC(ws);

            rtc.oncontextcreated = function(ctx) 
            {
                console.log("ctx", ctx);
            };

            rtc.oncontextchanged = function(change) 
            {
                console.log("change of context", change);
            };

            rtc.getUserMedia({audio:true,video:false}, function(result) 
            {   
                rtc.changeContext(myCtx);
                console.log("getUserMedia() success.", result);
            });

            rtc.onremotestream = function(event) {

                var randhash = Math.floor((Math.random() * 9999) + 1);
                randhash =  hex_md5(randhash);
                var videoTag = document.createElement('video');
                videoTag.setAttribute("id",randhash);
                videoTag.setAttribute("autoplay", "true");
                videoTag.setAttribute("poster", "images/user.png");
                document.getElementById('othersarea').appendChild(videoTag);
                attachMediaStream(videoTag, event.stream);

            }

            rtc.onlocalstream = function(stream) 
            {
                console.log("attach okay!");
                attachMediaStream(document.querySelector("#localVideo"), stream);
            };

            ws.subscribe("onChangeContextFailed", function(data) 
            {
                console.log(data);
            });

            rtc.bind(XSockets.WebRTC.Events.onPeerConnectionLost, function(peer) 
            {
                console.log('OnPeerConnectionLost', peer);
                    $('video').last().remove();
            }); 
        };

        var root = "<?php echo $root; ?>";
        fullURL = root.concat(myCtxURLVersion);

        function urlIntoBox(myCtxURLVersion) {
            var textbox = document.getElementById('txtSelect');
            textbox.value = fullURL;
        }

        function getNumVidsOnPage()
        {
            var videos = document.getElementsByTagName('video'),
            numVideos = videos.length;
        }

        urlIntoBox();

        var currVideos = getNumVidsOnPage();
        if (currVideos > 10)
        {
            window.location.href = "http://voice.gg?msg=That%20room%20has%20too%20many%users."; 
        }
   });
</script>

我怎样才能继续添加STUN功能以确保NAT用户也可以连接,因为他们在连接时遇到问题。 谢谢!

1 个答案:

答案 0 :(得分:2)

不知道您是否使用XSockets版本3或4,但是如果您查看XSockets WebRTC on GitHub,您会看到有一个关于配置的部分,其中显示了如何更改眩晕服务器。

来自github的例子:

var rtc = new XSockets.WebRTC(broker, {
    iceServers: [{
        url: 'stun:404.idonotexist.net'
    }],
    streamConstraints: {
        optional: [{
            'bandwidth': 500
        }]
}});