Permalink
Browse files

Allow direct calls from the user page.

  • Loading branch information...
1 parent ec5bd88 commit 4a9e27fd7e68960bc00f8a8ea193910a1d8d3144 Mark Banner committed with fqueze Nov 29, 2012
Showing with 135 additions and 29 deletions.
  1. +29 −0 app.js
  2. +3 −0 cloudfoundry.json
  3. +30 −0 static/serverConnections.js
  4. +1 −0 static/sidebar.htm
  5. +17 −25 static/sidebar.js
  6. +16 −2 static/user.html
  7. +34 −0 static/user.js
  8. +5 −2 static/worker.js
View
29 app.js
@@ -66,6 +66,29 @@ app.get("/events", function(req, res) {
Object.keys(users).length);
});
+app.post("/call", function(req, res) {
+ if (!req.body.assertion) {
+ res.send(500, "Invalid login request");
+ return;
+ }
+
+ verifyAssertion(req.body.assertion, audience, function(val) {
+ if (val) {
+ // XXX de-dupe with processRequest?
+ var channel = users[val];
+ if (!channel) {
+ res.send(400, "User not logged in for making calls");
+ return;
+ }
+
+ channelWrite(channel, "call", JSON.stringify(req.body));
+ res.send(200);
+ } else {
+ res.send(401, "Invalid Persona assertion");
+ }
+ });
+});
+
app.post("/login", function(req, res) {
if (!req.body.assertion) {
res.send(500, "Invalid login request");
@@ -76,7 +99,13 @@ app.post("/login", function(req, res) {
finishLogin(req.body.assertion);
} else {
verifyAssertion(req.body.assertion, audience, function(val) {
+ var noshow = req.body.noshow;
if (val) {
+ if (noshow) {
+ debugLog("User " + val + " logged in, but noshow specified");
+ res.send(200, val);
+ return;
+ }
finishLogin(val);
} else {
res.send(401, "Invalid Persona assertion");
View
@@ -0,0 +1,3 @@
+{
+ "ignoreNodeModules": true
+}
@@ -0,0 +1,30 @@
+function remoteLogin(options) {
+ $.ajax({
+ type: 'POST',
+ url: '/login',
+ data: options,
+ success: function(res, status, xhr) { signedIn(xhr.responseText); },
+ error: function(xhr, status, err) { alert("login failure" + err); }
+ });
+}
+
+function remoteLogout() {
+ $.ajax({
+ type: 'POST',
+ url: '/logout',
+ success: function(res, status, xhr) { signedOut(); },
+ error: function(xhr, status, err) { alert("logout failure" + err); }
+ });
+}
+
+function initiateCall(aAssertion, aUser) {
+ var options = {assertion: aAssertion, who: aUser };
+ $.ajax({
+ type: 'POST',
+ url: '/call',
+ data: options,
+ success: function(res, status, xhr) {},
+ error: function (xhr, status, err) { alert("call initiation failure " + err); }
+ });
+}
+
View
@@ -13,6 +13,7 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="./js/jquery-ui-1.9.1.custom.js"></script>
<script src="./sidebar.js" type="text/javascript"></script>
+<script src="./serverConnections.js" type="text/javascript"></script>
<script src="https://login.persona.org/include.js"></script>
<body onload="onLoad()">
View
@@ -2,25 +2,6 @@ var gContacts = {};
var gChats = {};
var gUsername = "";
-function remoteLogin(options) {
- $.ajax({
- type: 'POST',
- url: '/login',
- data: options,
- success: function(res, status, xhr) { signedIn(xhr.responseText); },
- error: function(xhr, status, err) { alert("login failure" + err); }
- });
-}
-
-function remoteLogout() {
- $.ajax({
- type: 'POST',
- url: '/logout',
- success: function(res, status, xhr) { },
- error: function(xhr, status, err) { alert("logout failure" + err); }
- });
-}
-
function onLoad() {
var worker = navigator.mozSocial.getWorker();
if (!worker) {
@@ -42,10 +23,13 @@ function onLoad() {
}
function onContactClick(aEvent) {
- var to = aEvent.target.innerHTML;
- openChat(to, function(aWin) {
+ callPerson(aEvent.target.innerHTML);
+}
+
+function callPerson(aPerson) {
+ openChat(aPerson, function(aWin) {
var pc = new mozRTCPeerConnection();
- var win = gChats[to].win;
+ var win = gChats[aPerson].win;
pc.onaddstream = function(obj) {
var doc = win.document;
var type = obj.type;
@@ -61,13 +45,13 @@ function onContactClick(aEvent) {
alert("sender onaddstream of unknown type, obj = " + obj.toSource());
}
};
- setupDataChannel(true, pc, to);
- gChats[to].pc = pc;
+ setupDataChannel(true, pc, aPerson);
+ gChats[aPerson].pc = pc;
getAudioVideo(aWin, pc, function() {
pc.createOffer(function(offer) {
pc.setLocalDescription(offer, function() {
$.ajax({type: 'POST', url: '/offer',
- data: {to: to, from: gUsername, request: JSON.stringify(offer)}});},
+ data: {to: aPerson, from: gUsername, request: JSON.stringify(offer)}});},
function(err) { alert("setLocalDescription failed: " + err); });
}, function(err) { alert("createOffer failed: " + err); });
});
@@ -150,6 +134,9 @@ function signedIn(aEmail) {
gContacts[aEmail] = $("<li>"); // Avoid displaying the user in the contact list.
}
+function signedOut() {
+}
+
function signout() {
navigator.id.logout();
@@ -335,6 +322,11 @@ function setupEventSource() {
pc.connectDataConnection(5000,5001);
}, function(err) {alert("failed to setRemoteDescription with answer, " + err);});
}, false);
+
+ source.addEventListener("call", function(e) {
+ var data = JSON.parse(e.data);
+ callPerson(data.who);
+ }, false);
}
function userIsConnected(userdata) {
View
@@ -1,10 +1,24 @@
<html>
<head>
- <title>Matey</title>
+ <title>Matey</title>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+ <script src="./js/jquery-ui-1.9.1.custom.js"></script>
+ <script src="./user.js" type="text/javascript"></script>
+ <script src="./serverConnections.js" type="text/javascript"></script>
+ <script src="https://login.persona.org/include.js"></script>
</head>
-<body>
+<body onload="onLoad()">
<img src="./user.png"/>
+<div id="header">
+ <div id="useridbox" style="display:none"></div>
+ <div id="nouserid">Welcome!</div>
+</div>
+<form>
+<input type="text" id="contactEmail"/>
+<input type="button" value="Call" onclick="startCall();"/>
+</form>
+
</body>
</html>
View
@@ -0,0 +1,34 @@
+function signedIn(aEmail) {
+ $("#useridbox").text("Welcome " + aEmail + "!");
+ $("#useridbox").show();
+ $("#nouserid").hide();
+}
+
+function signedOut() {
+ $("#useridbox").text("");
+ $("#useridbox").hide();
+ $("#nouserid").show();
+}
+
+// Did Persona really intend for it to be this way, what happens when it expires?
+var gUserAssertion;
+
+function startCall() {
+ initiateCall(gUserAssertion, document.getElementById("contactEmail").value);
+}
+
+function onLoad() {
+ if (navigator.id) {
+ navigator.id.watch({
+ loggedInUser: null,
+ onlogin: function(assertion) {
+ gUserAssertion = assertion;
+ remoteLogin({assertion: assertion, noshow: true});
+ },
+ onlogout: function() {
+ gUserAssertion = null;
+ remoteLogout();
+ }
+ });
+ }
+}
View
@@ -16,8 +16,8 @@ function broadcast(topic, payload)
{
// we need to broadcast to all ports connected to this shared worker
for (var i = 0; i < _broadcastReceivers.length; i++) {
- //log("about to broadcast to " + _broadcastReceivers[i]);
- _broadcastReceivers[i].postMessage({topic: topic, data: payload});
+ //log("about to broadcast to " + _broadcastReceivers[i] + "\n");
+ _broadcastReceivers[i].postMessage({topic: topic, data: payload});
}
}
@@ -67,6 +67,9 @@ onconnect = function(e) {
var userData = {};
// Messages from the sidebar and chat windows:
var handlers = {
+ 'worker.connected': function(port, msg) {
+ log("worker.connected");
+ },
'worker.reload': function(port, msg) {
broadcast(msg.topic, msg.data);
userData = {};

0 comments on commit 4a9e27f

Please sign in to comment.