Created
August 5, 2014 17:20
-
-
Save ToeJamson/b985953b604292f83499 to your computer and use it in GitHub Desktop.
encrypted AngularJS
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
<!doctype html> | |
<html> | |
<head> | |
<script src="https://cdn.pubnub.com/pubnub.min.js"></script> | |
<script src="https://cdn.pubnub.com/pubnub-crypto.min.js"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script> | |
<script src="//code.jquery.com/jquery-1.10.1.min.js"></script> | |
<script src="http://pubnub.github.io/angular-js/scripts/pubnub-angular.js"></script> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> | |
</head> | |
<body> |
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
// Register for presence events (optional) | |
$rootScope.$on(PubNub.ngPrsEv($scope.channel), function(ngEvent, payload) { | |
$scope.$apply(function() { | |
$scope.users = PubNub.ngListPresence($scope.channel); | |
}); | |
}); | |
// Pre-Populate the user list (optional) | |
PubNub.ngHereNow({ | |
channel: $scope.channel | |
}); |
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
}); | |
</script> | |
</body> | |
</html> |
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
// we must call angular "bootstrap" since we're running two separate | |
// angular apps for normal/encrypted views | |
angular.bootstrap($('#unencrypted'),['PubNubAngularUnencryptedApp']); |
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
<div class="container" ng-app="PubNubAngularApp" ng-controller="EncryptedChatCtrl"> |
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
<h4>Online Users</h4> | |
<ul> | |
<li ng-repeat="user in users">{{user}}</li> | |
</ul> |
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
<h4>Chat History {{messages.length}}</h4> |
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
<form ng-submit='publish()'> | |
<input type="text" ng-model='newMessage' /> | |
<input type="submit" value="Send" /> | |
</form> |
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
<div class="well"> | |
<ul> | |
<li ng-repeat="message in messages">{{message}}</li> | |
</ul> | |
</div> |
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
angular.module('PubNubAngularApp', ["pubnub.angular.service"]) | |
.controller('ChatCtrl', function($rootScope, $scope, $location, PubNub) { | |
// make up a user id (you probably already have this) | |
$scope.userId = "User " + Math.round(Math.random() * 1000); | |
// make up a channel name | |
$scope.channel = 'The Angular ENCRYPTED Channel'; | |
// pre-populate any existing messages (just an AngularJS scope object) | |
$scope.messages = ['Welcome to ' + $scope.channel]; |
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
if (!$rootScope.initialized) { | |
// Initialize the PubNub service | |
PubNub.init({ | |
subscribe_key: 'demo', | |
publish_key: 'demo', | |
cipher_key: 'changeme-noireallymeanit', /* adds a cipher key for secure message encryption (CHANGE THIS!) */ | |
uuid:$scope.userId | |
}); | |
$rootScope.initialized = true; | |
} |
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
// 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.userId + "] " + $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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment