Permalink
Browse files

Begin client-side integration of Transport

Still to do:

- Extend UI to support user log in
- Extend UI to support user name specification in calls
- Extend UI to support accepting/rejecting calls
- Implement custom message to properly support hanging up
- Implement custom message to support sharing of ICE candidates
  • Loading branch information...
1 parent 13b9546 commit df1bae359ed866c17b7993861fb2873355cf0018 @jugglinmike jugglinmike committed Mar 25, 2013
Showing with 85 additions and 50 deletions.
  1. +81 −50 prototype/public/scripts/app.js
  2. +4 −0 prototype/public/scripts/require-config.js
View
131 prototype/public/scripts/app.js
@@ -1,10 +1,10 @@
require([
- 'modules/nder', 'modules/pc', 'modules/layout', 'backbone'
- ], function(Nder, PC, Layout, Backbone) {
+ 'modules/transport', 'modules/pc', 'modules/layout', 'backbone'
+ ], function(Transport, PC, Layout, Backbone) {
'use strict';
var config = {
- socketServer: window.location.host,
+ socketServer: 'ws://' + window.location.host,
pcConfig: {
iceServers: [
{ url: 'stun:stun.l.google.com:19302' },
@@ -27,11 +27,6 @@ require([
OfferToReceiveVideo: true
}
};
- var setLocalAndSendMessage = function(sessionDescription) {
- this.setLocalDescription(sessionDescription);
- console.log('Sending SDP:', sessionDescription);
- nder.send(sessionDescription);
- };
function createOfferFailed() {
console.error('Create Answer failed');
@@ -48,17 +43,27 @@ require([
});
layout.render();
layout.on('connectRequest', function(stream) {
- if (!pc.isActive() && nder.is('open')) {
+ // TODO: Derive target user from application state
+ var targetUser = 'creationix';
+ if (!pc.isActive() && transport.state === 'OPEN') {
+
pc.init(config.pcConfig);
pc.addStream(stream);
pc.createOffer(
- setLocalAndSendMessage,
+ function(sessionDescription) {
+ this.setLocalDescription(sessionDescription);
+ transport.peerLocationFind(targetUser, {
+ session: sessionDescription,
+ userName: userName
+ });
+ },
createOfferFailed,
mediaConstraints);
}
});
layout.on('hangup', function() {
- nder.send({ type: 'bye' });
+ // TODO: implement `Transport#bye` method (or similar)
+ // transport.bye();
pc.destroy();
});
pc.on('addstream', function(stream) {
@@ -71,48 +76,74 @@ require([
});
pc.on('ice', function(msg) {
console.log('Sending ICE candidate:', msg);
- nder.send(msg);
+ // TODO: implement `Transport#sendIce` method (or similar)
+ // transport.sendIce(msg);
});
- var nder = new Nder({
- socketAddr: config.socketServer,
- handlers: {
- offer: function(msg) {
- console.log('Received offer...');
- if (!pc.isActive()) {
- pc.init(config.pcConfig);
- // TODO: Refactor so signalling service is not so tightly-coupled to
- // the layout.
- pc.addStream(layout.localStreamView.getStream());
- }
- console.log('Creating remote session description:', msg);
- pc.setRemoteDescription(msg);
- console.log('Sending answer...');
- pc.createAnswer(setLocalAndSendMessage,
- createAnswerFailed, mediaConstraints);
- },
- answer: function(msg) {
- if (!pc.isActive()) {
- return;
- }
- console.log('Received answer. Setting remote session description:',
- msg);
- pc.setRemoteDescription(msg);
- },
- candidate: function(msg) {
- if (!pc.isActive()) {
- return;
- }
- console.log('Received ICE candidate:', msg);
- pc.addIceCandidate(msg);
- },
- bye: function() {
- if (!pc.isActive()) {
- return;
- }
- console.log('Received bye');
- pc.destroy();
+ var transport = new Transport({
+ invite: function(request) {
+ var blob = request && request.username && request.username.blob;
+ var remoteSession;
+ if (!blob) {
+ console.error('No blob found. Ignoring invite.');
+ return;
+ }
+ remoteSession = blob.session;
+ if (!remoteSession) {
+ console.error('Remote session not specified. Ignoring invite.');
+ return;
}
+
+ // TODO: Prompt user to accept/reject call (instead of blindly accepting)
+ // and move following logic into "Accept" handler.
+ console.log('Receiving call from ' + blob.userName +
+ '. Would you like to answer?');
+
+ if (!pc.isActive()) {
+ pc.init(config.pcConfig);
+ // TODO: Refactor so transport is not so tightly-coupled to the layout.
+ // This should also allow recieving calls without sharing the local
+ // stream.
+ pc.addStream(layout.localStreamView.getStream());
+ }
+ console.log('Creating remote session description:', remoteSession);
+ pc.setRemoteDescription(remoteSession);
+ console.log('Sending answer...');
+ pc.createAnswer(function(sessionDescription) {
+ this.setLocalDescription(sessionDescription);
+ },
+ createAnswerFailed, mediaConstraints);
}
+ // TODO: Implement `ice` message (or similar)
+ /*ice: function(candidate) {
+ if (!pc.isActive()) {
+ return;
+ }
+ console.log('Received ICE candidate:', candidate);
+ pc.addIceCandidate(candidate);
+ }*/
});
+ // Infer username from 'username' query string parameter (default to
+ // 'creationx') and immediately initiate a connection.
+ // TODO: First prompt user for name, then initiate a connection.
+ var userName = 'creationix';
+ window.location.search
+ // Remove leading '?'
+ .slice(1)
+ .split('&')
+ .forEach(function(pair) {
+ pair = pair.split('=');
+ if (pair[0] === 'username') {
+ userName = pair[1];
+ }
+ });
+ transport.open(new WebSocket(config.socketServer))
+ .then(function() {
+ return transport.sessionCreate(userName);
+ })
+ .then(function() {
+ console.log('Logged in!');
+ // TODO: Update UI to reflect logged-in state.
+ }, console.error.bind(console));
+
});
View
4 prototype/public/scripts/require-config.js
@@ -6,9 +6,13 @@ require.config({
backbone: 'lib/backbone',
layoutmanager: 'lib/backbone.layoutmanager',
text: 'lib/text',
+ q: 'lib/q',
templates: '../templates'
},
shim: {
+ transport: {
+ deps: ['q']
+ },
_: {
exports: '_'
},

0 comments on commit df1bae3

Please sign in to comment.