-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpresence.js
302 lines (261 loc) · 8.06 KB
/
presence.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
define(function (require) {
// Message type constants
var msgInit = 0;
var msgListUsers = 1;
var msgCreateSharedActivity = 2;
var msgListSharedActivities = 3;
var msgJoinSharedActivity = 4;
var msgLeaveSharedActivity = 5;
var msgOnConnectionClosed = 6;
var msgOnSharedActivityUserChanged = 7;
var msgSendMessage = 8;
var msgListSharedActivityUsers = 9;
// Array for callbacks on each type
var callbackArray = [];
// User and shared info storage
var userInfo = null;
var sharedInfo = null;
// Connection object
function SugarPresence() {
// Init callbacks
var emptyCallback = function() {};
var listUsersCallback = emptyCallback;
var createSharedActivityCallback = emptyCallback;
var listSharedActivityCallback = emptyCallback;
var joinSharedActivity = emptyCallback;
var leaveSharedActivity = emptyCallback;
var onConnectionClosed = emptyCallback;
var onSharedActivityUserChanged = emptyCallback;
var receivedDataCallback = emptyCallback;
var listSharedActivityUsersCallback = emptyCallback;
callbackArray = [emptyCallback, listUsersCallback, createSharedActivityCallback,
listSharedActivityCallback, joinSharedActivity, leaveSharedActivity,
onConnectionClosed, onSharedActivityUserChanged, receivedDataCallback,
listSharedActivityUsersCallback
];
this.socket = null;
// Handle message received from server
this.registerMessageHandler = function() {
// Get message content
this.socket.onmessage = function(event) {
// Convert message to JSON
var edata = event.data;
try {
var json = JSON.parse(edata);
} catch (e) {
console.log('Presence API error, this doesn\'t look like a valid JSON: ', edata);
return;
}
// Call the matching callback
if (json.type < callbackArray.length)
callbackArray[json.type](json.data);
else
console.log('Presence API error, unknown callback type:'+json.type);
};
}
// Register user to the server
this.registerUser = function() {
this.socket.send(JSON.stringify(this.userInfo));
}
}
// Create presence object
var presence = new SugarPresence();
// Test if connected to network
SugarPresence.prototype.isConnected = function() {
return (this.socket != null && this.socket.readyState == 1);
}
// Get user info
SugarPresence.prototype.getUserInfo = function() {
return this.userInfo;
}
// Get shared activity info
SugarPresence.prototype.getSharedInfo = function() {
return this.sharedInfo;
}
// Get settings
function getSugarSettings(callback) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
chrome.storage.local.get('sugar_settings', function(values) {
callback(values.sugar_settings);
});
} else {
callback(localStorage.sugar_settings);
}
}
// Join network function
SugarPresence.prototype.joinNetwork = function(callback) {
// Check WebSocket support
if (!window.WebSocket){
that.socket = null;
console.log('WebSocket not supported');
callback({code: -1}, presence);
}
// Get server settings
var that = this;
getSugarSettings(function (sugar_settings) {
// Get server name
var server = location.hostname;
var port = 8039;
var secure = (location.protocol == 'https:');
if (sugar_settings) {
var sugarSettings = JSON.parse(sugar_settings);
if (sugarSettings.server) {
server = sugarSettings.server.url.substring(sugarSettings.server.url.indexOf("://")+3);
secure = (sugarSettings.server.url.indexOf("https://") == 0);
var endName = server.indexOf(':')
if (endName == -1) endName = server.indexOf('/');
if (endName == -1) endName = server.length;
server = server.substring(0, endName);
port = sugarSettings.server.presence;
}
}
// Connect to server
if (!server) {
that.socket = null;
console.log('No server available');
callback({code: -2}, presence);
return;
}
that.socket = new WebSocket((secure ? 'wss://' : 'ws://')+server+':'+port);
that.socket.onerror = function(error) {
console.log('WebSocket Error: ' + error);
callback(error, presence);
that.socket = null;
};
// When connection open, send user info
that.socket.onopen = function(event) {
var sugarSettings = JSON.parse(sugar_settings);
that.userInfo = {
name: sugarSettings.name,
networkId: sugarSettings.networkId,
colorvalue: sugarSettings.colorvalue
};
that.registerMessageHandler();
that.registerUser();
callback(null, presence);
};
// When connection closed, call closed callback
that.socket.onclose = function(event) {
callbackArray[msgOnConnectionClosed](event);
};
});
}
// Leave network
SugarPresence.prototype.leaveNetwork = function() {
if (!this.isConnected())
return;
this.socket.close();
this.socket = null;
}
// List all users. Will receive an array of users.
SugarPresence.prototype.listUsers = function(callback) {
if (!this.isConnected())
return;
// Register call back
callbackArray[msgListUsers] = callback;
// Send list user message
var sjson = JSON.stringify({
type: msgListUsers
});
this.socket.send(sjson);
}
// List users connected to a group. Will receive an array of users.
SugarPresence.prototype.listSharedActivityUsers = function(group, callback) {
if (!this.isConnected())
return;
// Register call back
var that = this;
callbackArray[msgListSharedActivityUsers] = function(data) {
callback(data);
}
// Send list user message
var sjson = JSON.stringify({
type: msgListSharedActivityUsers,
group: group
});
this.socket.send(sjson);
}
// Create a shared activity. Will receive a unique group id.
SugarPresence.prototype.createSharedActivity = function(activityId, callback) {
if (!this.isConnected())
return;
// Register call back
var that = this;
callbackArray[msgCreateSharedActivity] = function(data) {
that.sharedInfo = { id: data };
callback(data);
}
// Send create shared activity message
var sjson = JSON.stringify({
type: msgCreateSharedActivity,
activityId: activityId
});
this.socket.send(sjson);
}
// List all shared activities. Will receive an array of each shared activities and users connected
SugarPresence.prototype.listSharedActivities = function(callback) {
if (!this.isConnected())
return;
// Register call back
callbackArray[msgListSharedActivities] = callback;
// Send list shared activities message
var sjson = JSON.stringify({
type: msgListSharedActivities
});
this.socket.send(sjson);
}
// Join a shared activity. Will receive group properties or null
SugarPresence.prototype.joinSharedActivity = function(group, callback) {
if (!this.isConnected())
return;
// Register call back
var that = this;
callbackArray[msgJoinSharedActivity] = function(data) {
that.sharedInfo = data;
callback(data);
}
// Send join shared activity message
var sjson = JSON.stringify({
type: msgJoinSharedActivity,
group: group
});
this.socket.send(sjson);
}
// Leave shared activities
SugarPresence.prototype.leaveSharedActivity = function(group, callback) {
if (!this.isConnected())
return;
// Register call back
callbackArray[msgLeaveSharedActivity] = callback; // BUG: This callback is never called
// Send leave shared activity message
var sjson = JSON.stringify({
type: msgLeaveSharedActivity,
group: group
});
this.socket.send(sjson);
}
// Register connection closed event
SugarPresence.prototype.onConnectionClosed = function(callback) {
callbackArray[msgOnConnectionClosed] = callback;
}
// Register shared activity user changed event
SugarPresence.prototype.onSharedActivityUserChanged = function(callback) {
callbackArray[msgOnSharedActivityUserChanged] = callback;
}
// Send message to a group
SugarPresence.prototype.sendMessage = function(group, data) {;
if (!this.isConnected())
return;
var sjson = JSON.stringify({
type: msgSendMessage,
group: group,
data: data
});
this.socket.send(sjson);
}
// Register data received message
SugarPresence.prototype.onDataReceived = function(callback) {
callbackArray[msgSendMessage] = callback;
}
return presence;
});