//https://shanetully.com/2014/09/a-dead-simple-webrtc-example/
//https://github.com/muaz-khan/WebRTC-Experiment/tree/master/demos

var peer = new Peer('pick-an-id', {key: 'myapikey'}); 
// You can pick your own id or omit the id if you want to get a random one from the server.

//Data connections
//Connect

var conn = peer.connect('another-peers-id');
conn.on('open', function(){
  conn.send('hi!');
});

//Receive

peer.on('connection', function(conn) {
  conn.on('data', function(data){
    // Will print 'hi!'
    console.log(data);
  });
});




// web RTC

    function startRTC() {
        // for each in userlist start new RTC connection and send request to initialize it
        console.log("starting RTC connections");
        users = document.getElementById("userlist").getElementsByTagName("li")
        for (var i=0; i < users.length; i++) {
            user = users[i].innerHTML.trim();
            console.log("user " + user);
            CurrentPeerConnections[user] = new RTCPeerConnection(MyRTCConfiguration);
            CurrentPeerConnections[user].createDataChannel("livechat");
            CurrentPeerConnections[user].createOffer(function(offer) {
                CurrentPeerConnections[user].setLocalDescription(new RTCSessionDescription(offer), function() {
                    // send the offer to a server to be forwarded to the friend you're calling.
                    ws.send(user + ": " + JSON.stringify(offer));
                    console.log(user + " offer sent");
                });
            });
            
            console.log("next user")
        }
        
    }
