1

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

这个 JS 创建了我的连接。我在运行单声道的 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 用户也可以连接,因为目前他们在连接时遇到问题。谢谢!

4

1 回答 1

2

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

来自 github 的示例:

var rtc = new XSockets.WebRTC(broker, {
    iceServers: [{
        url: 'stun:404.idonotexist.net'
    }],
    streamConstraints: {
        optional: [{
            'bandwidth': 500
        }]
}});
于 2014-10-22T14:17:55.800 回答