-
Notifications
You must be signed in to change notification settings - Fork 57
/
crypto_1.0.x.html
141 lines (119 loc) · 4.14 KB
/
crypto_1.0.x.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!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>
<div class="container" id="encrypted" ng-app="PubNubAngularEncryptedApp" ng-controller="EncryptedChatCtrl">
<h4>Online Users</h4>
<ul>
<li ng-repeat="user in users">{{user}}</li>
</ul>
<br />
<h4>Chat History ({{messages.length}})</h4>
<form ng-submit='publish()'>
<input type="text" ng-model='newMessage' />
<input type="submit" value="Send" />
</form>
<br />
<div class="well">
<ul>
<li ng-repeat="message in messages">{{message}}</li>
</ul>
</div>
</div>
<div class="container" id="unencrypted" ng-app="PubNubAngularUnencryptedApp" ng-controller="UnencryptedChatCtrl">
<h4>Encrypted Chat History ({{messages.length}})</h4>
<div class="well">
<ul>
<li ng-repeat="message in messages">{{message}}</li>
</ul>
</div>
</div>
<script>
//
// Set Up Your Angular Module & Controller(s)
//
angular.module('PubNubAngularEncryptedApp', ["pubnub.angular.service"])
.controller('EncryptedChatCtrl', 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];
if (!$rootScope.initialized) {
// Initialize the PubNub service
PubNub.init({
subscribe_key: 'demo',
publish_key: 'demo',
cipher_key: 'changeme-noireallymeanit',
uuid:$scope.userId
});
$rootScope.initialized = 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.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);
});
});
// 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
});
});
angular.module('PubNubAngularUnencryptedApp', ["pubnub.angular.service"])
.controller('UnencryptedChatCtrl', 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 + ' contents ENCRYPTED!'];
if (!$rootScope.initialized) {
// Initialize the PubNub service
// NOTE - we don't provide a cipher_key since we want to see messages in encrypted form!
PubNub.init({
subscribe_key: 'demo',
publish_key: 'demo',
uuid:$scope.userId
});
$rootScope.initialized = true;
}
// Subscribe to the Channel
PubNub.ngSubscribe({ channel: $scope.channel });
// Register for message events
$rootScope.$on(PubNub.ngMsgEv($scope.channel), function(ngEvent, payload) {
$scope.$apply(function() {
$scope.messages.push(payload.message);
});
});
});
// we must call angular "bootstrap" since we're running two separate
// angular apps for normal/encrypted views
angular.bootstrap($('#unencrypted'),['PubNubAngularUnencryptedApp']);
</script>
</body>
</html>