Created
March 14, 2014 17:07
-
-
Save EricDavies/9552120 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var selfEasyrtcid = ""; | |
var connectTime; | |
function connect() { | |
easyrtc.setRoomOccupantListener(convertListToButtons); | |
easyrtc.easyApp("easyrtc.audioVideo", "selfVideo", ["callerVideo"], loginSuccess, loginFailure); | |
easyrtc.setOnHangup(function(easyrtcid, slot) { | |
easyrtc.updatePresence("chat", ""); | |
}); | |
} | |
function clearConnectList() { | |
var otherClientDiv = document.getElementById('otherClients'); | |
while (otherClientDiv.hasChildNodes()) { | |
otherClientDiv.removeChild(otherClientDiv.lastChild); | |
} | |
} | |
function convertListToButtons (roomName, data, isPrimary) { | |
var first = null; | |
var second = null; | |
var client; | |
// find the first two occupants of the room, other than ourselves | |
for( client in data) { | |
if( !first ) { | |
first = client; | |
} | |
else if( data[first].roomJoinTime > data[client].roomJoinTime) { | |
second = first; | |
first = client; | |
} | |
else if( !second || data[second].roomJoinTime > data[client].roomJoinTime) { | |
second = client; | |
} | |
} | |
if(!first) { | |
console.log("nobody to talk to yet"); | |
return; // nobody to talk to | |
} | |
if( data[first].presence.show === "dnd") { // already in conversation | |
console.log("first person party in room (" + first +") already in call"); | |
return; | |
} | |
if( second && connectTime >= data[second].roomJoinTime) { | |
if( connectTime != data[second].roomJoinTime || second > selfEasyrtcid) { | |
console.log("we're the third person in the room."); | |
return; // we're not the second person in the room. | |
} | |
} | |
console.log("we're the first or second person in the room"); | |
// no sense have both parties try to call each other at the same time | |
if( selfEasyrtcid > first) { | |
console.log("calling " + first); | |
performCall(first); | |
} | |
else { | |
console.log("expect to be called by the first person in the room"); | |
} | |
} | |
function performCall(otherEasyrtcid) { | |
easyrtc.hangupAll(); | |
var successCB = function() { | |
easyrtc.updatePresence("dnd", ""); // denote we are in a conversation | |
}; | |
var failureCB = function() {}; | |
easyrtc.call(otherEasyrtcid, successCB, failureCB); | |
} | |
function loginSuccess(easyrtcid) { | |
connectTime = (new Date()).getTime(); | |
console.log("connect time is", connectTime); | |
selfEasyrtcid = easyrtcid; | |
document.getElementById("iam").innerHTML = "I am " + easyrtc.cleanId(easyrtcid); | |
} | |
function loginFailure(errorCode, message) { | |
easyrtc.showError(errorCode, message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment