1

I'm quite new to this. How would I go about adding STUN functionality to my existing XSockets.NET WebRTC Javascript code?

This JS creates my connections. I am running the signalling server separate on a linux machine running mono.

<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>

How could I go ahead and add STUN functionality to make sure NAT users can also connect because at the moment they are having issues connecting.. Thanks!

mpromonet
  • 11,326
  • 43
  • 62
  • 91
KriiV
  • 1,882
  • 4
  • 25
  • 43

1 Answers1

2

Do not know if you are using XSockets version 3 or 4, but if you take a look at XSockets WebRTC on GitHub you will see that there is a section about configuration that shows how to change the stun server.

Example from github:

var rtc = new XSockets.WebRTC(broker, {
    iceServers: [{
        url: 'stun:404.idonotexist.net'
    }],
    streamConstraints: {
        optional: [{
            'bandwidth': 500
        }]
}});
Uffe
  • 2,275
  • 1
  • 13
  • 9
  • Thanks! Do you have any STUN servers you recommend? – KriiV Oct 22 '14 at 14:27
  • stun.google.com is the default one, but you can add any stun server. If you need turn servers you can consult the xsockets developer forum. – Uffe Oct 22 '14 at 14:29
  • It appears I'm running version 3.0 – KriiV Oct 23 '14 at 00:22
  • Yeah, I can see that when I take a closer look. Version 3 with WebRTC is great for hobby/private stuff, but version 4 is much faster and have some issues like connection glitches fixed. You can add stun to version 3 as well so you can still achieve what you asked for. – Uffe Oct 23 '14 at 05:15
  • Would adding STUN to v3 be done with the above code? – KriiV Oct 23 '14 at 07:27