-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
85 lines (71 loc) · 2.31 KB
/
demo.js
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
angular.module('app', ['realtime'])
.controller('appCtrl', function($scope, $realtime) {
$realtime.setCredentials('JVE6un', 'my-auth-token-if-using-presence');
$scope.connect = function() {
$realtime.connect();
};
$scope.disconnect = function() {
$realtime.disconnect();
};
$scope.subscribe = function(channel) {
$realtime.subscribe(channel);
};
$scope.unsubscribe = function(channel) {
$realtime.unsubscribe(channel);
};
$scope.send = function(channel, message) {
$realtime.send(channel, { user: $scope.name, msg: message});
};
$scope.channels = [
{
id: 'angularRealtime1',
messages: []
},
{
id: 'angularRealtime2',
messages: []
},
{
id: 'angularRealtime3',
messages: []
}
];
$scope.isConnected = false;
$scope.$on('realtime:onConnected', function() {
_.each($scope.channels, function(channel) {
$scope.subscribe(channel.id);
});
$scope.$apply(function() {
$scope.isConnected = true;
});
});
$scope.$on('realtime:onDisconnected', function() {
$scope.$apply(function() {
$scope.isConnected = false;
});
});
$scope.$on('realtime:onException', function(event, ortc, exception) {
console.error('realtime:onException', exception);
});
$scope.$on('realtime:onReconnected', function() {
console.info('realtime:onReconnected');
});
// Channel Events
_.each($scope.channels, function(channel) {
$scope.$on('realtime:'+channel.id+':onSubscribed', function(event, ortc) {
$scope.$apply(function() {
_.find($scope.channels, { id: channel.id }).isSubscribed = true;
});
});
$scope.$on('realtime:'+channel.id+':onUnsubscribed', function(event, ortc) {
$scope.$apply(function() {
_.find($scope.channels, { id: channel.id }).isSubscribed = false;
});
});
$scope.$on('realtime:'+channel.id+':onMessage', function(event, message) {
$scope.$apply(function() {
_.find($scope.channels, { id: channel.id }).messages.push(message);
});
});
});
});