Created
April 3, 2015 07:56
-
-
Save absyah/e0124f43b5dc7fade5b4 to your computer and use it in GitHub Desktop.
angular and webrtc using pubnub
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
myApp.controller('DashboardIndexController', ['$rootScope','$scope', 'PubNub', '$state', '$auth', | |
function($rootScope, $scope, PubNub, $state, $auth) { | |
// http://www.pubnub.com/blog/angularjs-101-from-zero-to-angular-in-seconds/ | |
$scope.userEmail = $rootScope.user.email; | |
$scope.uuid = $rootScope.user.id; | |
$scope.channel = "Dashboard Channel"; | |
$scope.messages = ['Welcome to ' + $scope.channel]; | |
if (!$rootScope.chatInitialized) { | |
// Initialize the PubNub service | |
PubNub.init({ | |
subscribe_key : settings.pubnubKey('subKey'), | |
publish_key : settings.pubnubKey('pubKey'), | |
uuid : $scope.uuid | |
}); | |
$rootScope.chatInitialized = true; | |
} | |
// Subscribe to the Channel | |
PubNub.ngSubscribe({ channel: $scope.channel }); | |
// Create a publish() function in the scope | |
$scope.publish = function() { | |
PubNub.ngPublish({ | |
channel: $scope.channel, | |
message: "[" + $scope.userEmail + "] " + $scope.newMessage | |
}); | |
$scope.newMessage = ''; | |
}; | |
// Register for message events | |
$rootScope.$on(PubNub.ngMsgEv($scope.channel), function(ngEvent, payload) { | |
$scope.$apply(function() { | |
$scope.messages.push(payload.message); | |
}); | |
}); | |
//* Video * | |
// Video session | |
if (!$rootScope.phoneInitialized) { | |
$scope.phone = PHONE({ | |
number : $scope.uuid, | |
publish_key : settings.pubnubKey('subKey'), | |
subscribe_key : settings.pubnubKey('pubKey'), | |
ssl : true | |
}); | |
$rootScope.phoneInitialized = true; | |
} | |
$scope.call = function() { | |
// As soon as the phone is ready we can make calls | |
$scope.phone.ready(function(){ | |
// Dial a Number and get the Call Session | |
// For simplicity the phone number is the same for both caller/receiver. | |
// you should use different phone numbers for each user. | |
$scope.phoneSession = phone.dial('8'); | |
}); | |
}; | |
// When Call Comes In or is to be Connected | |
$scope.phone.receive(function(session){ | |
// Display Your Friend's Live Video | |
session.connected(function(session){ | |
PUBNUB.$('video-out').appendChild(session.video); | |
}); | |
}); | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment