forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketClient.ts
More file actions
476 lines (461 loc) · 23.7 KB
/
Copy pathWebSocketClient.ts
File metadata and controls
476 lines (461 loc) · 23.7 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/// <reference path='ReconnectingWebSocket.ts' />
module openflow {
// "use strict";
interface IHashTable<T> {
[key: string]: T;
}
declare var cordova: any;
declare var device: any;
declare var diagnostic: any;
export type QueuedMessageCallback = (msg: any) => any;
export type QueuedMessageStatusCallback = (msg: any, index: number, count: number) => any;
export class QueuedMessage {
constructor(message: any, cb: QueuedMessageCallback, status: QueuedMessageStatusCallback) {
this.id = message.id;
this.message = message;
this.cb = cb;
this.status = status;
}
public cb: QueuedMessageCallback;
public status: QueuedMessageStatusCallback;
public id: string;
public message: any;
}
export class WebSocketClient {
private _socketObject: ReconnectingWebSocket = null;
public stripe_api_key: string = "";
public domain: string = null;
public allow_personal_nodered: boolean = false;
public allow_user_registration: boolean = false;
public namespace: string = null;
public version: string = null;
public nodered_domain_schema: string = null;
private _url: string = null;
private static instance: WebSocketClient = null;
private _receiveQueue: SocketMessage[] = [];
private _sendQueue: SocketMessage[] = [];
public user: TokenUser = null;
public jwt: string = null;
public device: any = null;
public usingCordova: boolean = false;
public plugins: any = null;
public scanCount: number = 0;
public oneSignalId: string = null;
public location: any;
public websocket_package_size: number = 500;
static $inject = ["$rootScope", "$location", "$window"];
public messageQueue: IHashTable<QueuedMessage> = {};
constructor(public $rootScope: ng.IRootScopeService, public $location, public $window: any) {
try {
var path = this.$location.absUrl().split('#')[0];
// this.usingCordova = (path.indexOf("/android/") > -1 || path.indexOf("/ios/") > -1);
} catch (error) {
}
this.init();
}
onbackbutton() {
}
onPause() {
}
onResume() {
}
onMenuKeyDown() {
}
getids(oneSignal: any): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
oneSignal.getIds(function (ids) {
resolve(ids.userId);
});
});
}
getLocation(): Promise<any> {
return new Promise<any>(async (resolve, reject) => {
var onSuccess = function (position) {
var result = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
altitude: position.coords.altitude,
accuracy: position.coords.accuracy,
altitudeAccuracy: position.coords.altitudeAccuracy,
heading: position.coords.heading,
speed: position.coords.speed,
timestamp: position.coords.timestamp
}
console.debug('Latitude: ' + result.latitude + '\n' +
'Longitude: ' + result.longitude + '\n' +
'Altitude: ' + result.altitude + '\n' +
'Accuracy: ' + result.accuracy + '\n' +
'Altitude Accuracy: ' + result.altitudeAccuracy + '\n' +
'Heading: ' + result.heading + '\n' +
'Speed: ' + result.speed + '\n' +
'Timestamp: ' + result.timestamp + '\n');
resolve(result);
};
// onError Callback receives a PositionError object
//
function onError(error) {
reject(error);
console.error('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
var options = {
enableHighAccuracy: true,
timeout: 2000,
maximumAge: 0
};
try {
if (navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
} else {
reject(new Error("geolocation not installed!"));
}
} catch (error) {
reject(error);
}
});
}
isLocationAvailable(): Promise<boolean> {
return new Promise<boolean>(async (resolve, reject) => {
cordova.plugins.diagnostic.isLocationAvailable((isAvailable) => {
resolve(isAvailable);
}, (error) => {
reject(error);
});
});
}
isLocationAuthorized(): Promise<boolean> {
return new Promise<boolean>(async (resolve, reject) => {
cordova.plugins.diagnostic.isLocationAuthorized((authorized) => {
resolve(authorized);
}, (error) => {
reject(error);
});
});
}
requestLocationAuthorization(): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
cordova.plugins.diagnostic.requestLocationAuthorization(() => {
resolve();
}, (error) => {
reject(error);
});
});
}
scanForCordova() {
if (this.usingCordova == true) return;
try {
if (cordova !== undefined) {
this.usingCordova = true;
document.addEventListener("deviceready", async () => {
if ((window as any).plugins) {
this.plugins = (window as any).plugins;
var oneSignal = (window as any).plugins.OneSignal;
if (oneSignal) {
try {
console.debug("window.plugins.OneSignal exists");
const sender_id = "906036108091"; // google_project_number
const oneSignalAppId = "cfdefd08-d4ad-4593-8173-4ba4ebf4d67a"; // onesignal_app_id
var iosSettings = {};
iosSettings["kOSSettingsKeyAutoPrompt"] = true;
iosSettings["kOSSettingsKeyInAppLaunchURL"] = true;
console.debug("oneSignal.startInit");
oneSignal.startInit(oneSignalAppId, sender_id).
iOSSettings(iosSettings).
inFocusDisplaying(oneSignal.OSInFocusDisplayOption.Notification).
handleNotificationOpened(this.notificationOpenedCallback).
handleNotificationReceived(this.notificationReceivedCallback).
endInit();
this.oneSignalId = await this.getids(oneSignal);
} catch (error) {
console.error(error);
}
} else {
console.debug("Missing oneSignal plugin");
}
}
try {
if (device) {
console.debug("device exists");
this.device = device;
} else {
console.debug("device does NOT exists");
}
} catch (error) {
console.error(error);
}
document.addEventListener("pause", this.onPause, false);
document.addEventListener("resume", this.onResume, false);
document.addEventListener("menubutton", this.onMenuKeyDown, false);
document.addEventListener("backbutton", this.onbackbutton, false);
if (cordova.plugins && cordova.plugins.diagnostic) {
console.debug("Check if authorized for location");
var isAuthorized = await this.isLocationAuthorized();
if (!isAuthorized) {
console.debug("Not authorized for location is not , request authorization");
await this.requestLocationAuthorization();
}
var isAvailable = await this.isLocationAvailable();
if (!isAvailable) {
console.debug("Location is not available");
} else {
console.debug("Location is available, get current location");
this.location = await this.getLocation();
}
} else {
console.debug("diagnostic is missing");
}
this.$rootScope.$broadcast("cordovadetected");
});
} else {
console.debug("cordova not definded");
if (this.scanCount < (5 * 4)) {
this.scanCount++;
setTimeout(this.scanForCordova, 200);
}
}
} catch (error) {
console.debug("Failed locating cordova. " + error);
}
}
init() {
this.getJSON("/config", async (error: any, data: any) => {
var parser = document.createElement('a');
parser.href = data.wshost;
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
if (location.protocol == 'https:' && parser.protocol == "ws:") {
data.wshost = "wss://" + parser.hostname;
console.debug("new wshost: " + data.wshost);
if (parser.port != "80") {
data.wshost = "wss://" + parser.hostname + parser.port;
}
}
if (location.protocol == 'http:' && parser.protocol == "wss:") {
data.wshost = "ws://" + parser.hostname;
if (parser.port != "443") {
data.wshost = "ws://" + parser.hostname + parser.port;
}
}
console.debug("WebSocketClient::onopen: connecting to " + data.wshost);
this.domain = data.domain;
this.allow_personal_nodered = data.allow_personal_nodered;
this.allow_user_registration = data.allow_user_registration;
this.namespace = data.namespace;
this.nodered_domain_schema = data.nodered_domain_schema;
this.websocket_package_size = data.websocket_package_size;
this.version = data.version;
this.stripe_api_key = data.stripe_api_key;
this._socketObject = new ReconnectingWebSocket(data.wshost);
this._socketObject.onopen = (this.onopen).bind(this);
this._socketObject.onmessage = (this.onmessage).bind(this);
this._socketObject.onclose = (this.onclose).bind(this);
this._socketObject.onerror = (this.onerror).bind(this);
WebSocketClient.instance = this;
});
}
notificationOpenedCallback(notification) {
var state = notification.notification;
console.debug(JSON.stringify(state));
try {
//if (state.isAppInFocus) {
if (state.payload.additionalData.URL != undefined && state.payload.additionalData.URL != null && state.payload.additionalData.URL != "") {
window.location.href = state.payload.additionalData.URL;
}
if (state.payload.additionalData.customurl != undefined && state.payload.additionalData.customurl != null && state.payload.additionalData.customurl != "") {
window.location.href = state.payload.additionalData.customurl;
}
return;
//}
// {"isAppInFocus":true,"shown":true,"androidNotificationId":-1616162934,"displayType":0,"payload":{"notificationID":"aee1f7a2-2108-489d-a401-86dba6a1ad99","body":"Android 2019-06-02T20:58:25.876Z","additionalData":{"URL":"https://aiotdev-frontend.openrpa.dk/#/Alert/5cf25ad801530ae6396519b8"},"launchURL":"https://aiotdev-frontend.openrpa.dk/#/Alert/5cf25ad801530ae6396519b8","lockScreenVisibility":1,"fromProjectNumber":"906036108091","priority":0,"rawPayload":"{\"google.delivered_priority\":\"normal\",\"google.sent_time\":1559509106669,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"URL\\\":\\\"https:\\\\\\/\\\\\\/aiotdev-frontend.openrpa.dk\\\\\\/#\\\\\\/Alert\\\\\\/5cf25ad801530ae6396519b8\\\"},\\\"u\\\":\\\"https:\\\\\\/\\\\\\/aiotdev-frontend.openrpa.dk\\\\\\/#\\\\\\/Alert\\\\\\/5cf25ad801530ae6396519b8\\\",\\\"i\\\":\\\"aee1f7a2-2108-489d-a401-86dba6a1ad99\\\"}\",\"from\":\"906036108091\",\"alert\":\"Android 2019-06-02T20:58:25.876Z\",\"google.message_id\":\"0:1559509106674108%6c875f80f9fd7ecd\",\"notificationId\":-1616162934}"}}
} catch (error) {
console.error(error);
}
}
notificationReceivedCallback(state) {
// console.debug(JSON.stringify(state));
// try {
// //if (state.isAppInFocus) {
// if (state.payload.additionalData.URL != undefined && state.payload.additionalData.URL != null && state.payload.additionalData.URL != "") {
// window.location.href = state.payload.additionalData.URL;
// }
// if (state.payload.additionalData.customurl != undefined && state.payload.additionalData.customurl != null && state.payload.additionalData.customurl != "") {
// window.location.href = state.payload.additionalData.customurl;
// }
// return;
// //}
// // {"isAppInFocus":true,"shown":true,"androidNotificationId":-1616162934,"displayType":0,"payload":{"notificationID":"aee1f7a2-2108-489d-a401-86dba6a1ad99","body":"Android 2019-06-02T20:58:25.876Z","additionalData":{"URL":"https://aiotdev-frontend.openrpa.dk/#/Alert/5cf25ad801530ae6396519b8"},"launchURL":"https://aiotdev-frontend.openrpa.dk/#/Alert/5cf25ad801530ae6396519b8","lockScreenVisibility":1,"fromProjectNumber":"906036108091","priority":0,"rawPayload":"{\"google.delivered_priority\":\"normal\",\"google.sent_time\":1559509106669,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"URL\\\":\\\"https:\\\\\\/\\\\\\/aiotdev-frontend.openrpa.dk\\\\\\/#\\\\\\/Alert\\\\\\/5cf25ad801530ae6396519b8\\\"},\\\"u\\\":\\\"https:\\\\\\/\\\\\\/aiotdev-frontend.openrpa.dk\\\\\\/#\\\\\\/Alert\\\\\\/5cf25ad801530ae6396519b8\\\",\\\"i\\\":\\\"aee1f7a2-2108-489d-a401-86dba6a1ad99\\\"}\",\"from\":\"906036108091\",\"alert\":\"Android 2019-06-02T20:58:25.876Z\",\"google.message_id\":\"0:1559509106674108%6c875f80f9fd7ecd\",\"notificationId\":-1616162934}"}}
// } catch (error) {
// console.error(error);
// }
}
public connect(): void {
}
getJSON(url: string, callback: any): void {
var xhr: XMLHttpRequest = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "json";
xhr.onload = function (): void {
var status: number = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
}
onSignedin(callback) {
if (this.user !== null) {
callback(this.user);
return;
}
var cleanup = this.$rootScope.$on('signin', (event, data) => {
if (event && data) { }
cleanup();
callback(this.user);
});
}
onConnected(callback) {
if (this.connected) {
callback();
return;
}
var cleanup = this.$rootScope.$on('socketopen', (event, data) => {
if (event && data) { }
cleanup();
callback();
});
}
timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
private connected: boolean = false;
private onopen(evt: Event) {
// console.debug("WebSocketClient::onopen: connected");
this.$rootScope.$broadcast("socketopen");
setTimeout(this.scanForCordova.bind(this), 200);
this.connected = true;
}
private onclose(evt: CloseEvent): void {
var me: WebSocketClient = WebSocketClient.instance;
this.connected = false;
}
private onerror(evt: ErrorEvent): void {
var me: WebSocketClient = WebSocketClient.instance;
this.connected = false;
}
private onmessage(evt: MessageEvent): void {
var me: WebSocketClient = WebSocketClient.instance;
let msg: SocketMessage = SocketMessage.fromjson(evt.data);
me._receiveQueue.push(msg);
me.ProcessQueue.bind(me)();
}
public async Send<T>(message: Message, status: QueuedMessageStatusCallback = null): Promise<T> {
return new Promise<T>(async (resolve, reject) => {
this._Send(message, ((msg) => {
if (msg.error !== null && msg.error !== undefined) { console.error(message); return reject(msg.error); }
resolve(msg);
}).bind(this), status);
});
}
private _Send(message: Message, cb: QueuedMessageCallback, status: QueuedMessageStatusCallback): void {
var messages: string[] = this.chunkString(message.data, this.websocket_package_size);
if (messages === null || messages === undefined || messages.length === 0) {
var singlemessage: SocketMessage = SocketMessage.frommessage(message, "", 1, 0);
if (message.replyto === null || message.replyto === undefined) {
this.messageQueue[singlemessage.id] = new QueuedMessage(singlemessage, cb, status);
}
this._sendQueue.push(singlemessage);
return;
}
if (message.id === null || message.id === undefined) { message.id = Math.random().toString(36).substr(2, 9); }
for (let i: number = 0; i < messages.length; i++) {
var _message: SocketMessage = SocketMessage.frommessage(message, messages[i], messages.length, i);
this._sendQueue.push(_message);
}
if (message.replyto === null || message.replyto === undefined) {
this.messageQueue[message.id] = new QueuedMessage(message, cb, status);
}
this.ProcessQueue();
}
public chunkString(str: string, length: number): string[] {
if (str === null || str === undefined) {
return [];
}
// tslint:disable-next-line: quotemark
return str.match(new RegExp('.{1,' + length + '}', 'g'));
}
private ProcessQueue(): void {
let ids: string[] = [];
this._receiveQueue.forEach(msg => {
if (ids.indexOf(msg.id) === -1) { ids.push(msg.id); }
});
ids.forEach(id => {
var msgs: SocketMessage[] = this._receiveQueue.filter(function (msg: SocketMessage): boolean { return msg.id === id; });
msgs.sort((a, b) => a.index - b.index);
var first: SocketMessage = msgs[0];
if (first.count === msgs.length) {
if (msgs.length === 1) {
var singleresult: Message = Message.frommessage(first, first.data);
this._receiveQueue = this._receiveQueue.filter(function (msg: SocketMessage): boolean { return msg.id !== id; });
singleresult.Process(this);
} else {
var buffer: string = "";
msgs.forEach(msg => {
if (msg.data !== null && msg.data !== undefined) { buffer += msg.data; }
});
var result: Message = Message.frommessage(first, buffer);
this._receiveQueue = this._receiveQueue.filter(function (msg: SocketMessage): boolean { return msg.id !== id; });
result.Process(this);
}
this._receiveQueue = this._receiveQueue.filter(function (msg: SocketMessage): boolean { return msg.id !== id; });
} else {
var qm: QueuedMessage = this.messageQueue[first.replyto];
if (qm != null && qm != undefined) {
if (qm.status != null && qm.status != undefined) {
qm.status(first, msgs.length, first.count);
}
}
}
});
if (this._socketObject !== null && this._socketObject.readyState !== 1) {
this.connect();
setTimeout(() => {
this.ProcessQueue();
}, 1500);
return;
}
var sendcounter: number = 0;
while (this._sendQueue.length > 0 && sendcounter < 100) {
var msg = this._sendQueue[0];
try {
if (this._socketObject !== null && this._socketObject.readyState === 1) {
let id: string = msg.id;
this._socketObject.send(JSON.stringify(msg));
var qm: QueuedMessage = this.messageQueue[id];
if (qm != null && qm != undefined) {
if (qm.status != null && qm.status != undefined) {
qm.status(msg, msg.index, msg.count);
}
}
this._sendQueue.splice(0, 1);
// this._sendQueue = this._sendQueue.filter(function (msg: SocketMessage): boolean { return msg.id !== id; });
sendcounter++;
}
} catch (error) {
console.error(error);
}
}
if (this._sendQueue.length > 0) {
setTimeout(() => {
this.ProcessQueue();
}, 100);
}
}
}
}