-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-realtime.js
116 lines (95 loc) · 4.62 KB
/
angular-realtime.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
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
(function() {
'use strict';
angular.module('realtime', [])
.provider('$realtime', function() {
var applicationKey = null;
var authenticationToken = null;
var realtimeClusterUrl = 'https://ortc-developers.realtime.co/server/ssl/2.1';
var realtimeClient = null;
function Realtime($rootScope) {
this.setClusterUrl = function(clusterUrl) {
realtimeClient.setClusterUrl(clusterUrl);
};
this.setCredentials = function(key, token, connect) {
applicationKey = key;
authenticationToken = token;
if (!angular.isDefined(connect) || connect === true) {
this.connect();
}
};
this.connect = function() {
if (realtimeClient && !realtimeClient.getIsConnected()) {
realtimeClient.connect(applicationKey, authenticationToken);
}
};
this.disconnect = function() {
if (realtimeClient && realtimeClient.getIsConnected()) {
realtimeClient.disconnect();
}
};
this.send = function(channel, message) {
if (realtimeClient) {
if (typeof message !== 'string') {
message = JSON.stringify(message);
}
realtimeClient.send(channel, message);
}
};
this.subscribe = function(channel) {
if (realtimeClient && !realtimeClient.isSubscribed(channel)) {
realtimeClient.subscribe(channel, true, function (ortc, channel, message) {
try {
message = JSON.parse(message);
} catch (e) {
$rootScope.$broadcast('realtime:' + channel + ':jsonParseException', e);
}
$rootScope.$broadcast('realtime:' + channel + ':onMessage', message);
});
}
};
this.unsubscribe = function(channel) {
if (realtimeClient && realtimeClient.isSubscribed(channel)) {
realtimeClient.unsubscribe(channel);
}
};
if (typeof loadOrtcFactory !== 'undefined') {
loadOrtcFactory(IbtRealTimeSJType, function (factory, error) {
if (error != null) {
$rootScope.$broadcast('realtime:loadOrtcFactoryError', error);
} else {
if (factory != null) {
realtimeClient = factory.createClient();
realtimeClient.setClusterUrl(realtimeClusterUrl);
realtimeClient.onConnected = function (ortc) {
$rootScope.$broadcast('realtime:onConnected', ortc);
};
realtimeClient.onDisconnected = function (ortc) {
$rootScope.$broadcast('realtime:onDisconnected', ortc);
};
realtimeClient.onException = function (ortc, exception) {
$rootScope.$broadcast('realtime:onException', ortc, exception);
};
realtimeClient.onReconnected = function (ortc) {
$rootScope.$broadcast('realtime:onReconnected', ortc);
};
realtimeClient.onReconnecting = function (ortc) {
$rootScope.$broadcast('realtime:onReconnecting', ortc);
};
realtimeClient.onSubscribed = function (ortc, channel) {
$rootScope.$broadcast('realtime:' + channel + ':onSubscribed');
};
realtimeClient.onUnsubscribed = function (ortc, channel) {
$rootScope.$broadcast('realtime:' + channel + ':onUnsubscribed', ortc);
};
}
}
});
} else {
console.error('angular-realtime', 'loadOrtcFactory is undefined - make sure you\'re connected to the internet and that you can access http://messaging-public.realtime.co/js/2.1.0/ortc.js');
}
}
this.$get = ['$rootScope', function($rootScope) {
return new Realtime($rootScope);
}];
});
}());