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
247 lines (243 loc) · 10.9 KB
/
Copy pathWebSocketClient.ts
File metadata and controls
247 lines (243 loc) · 10.9 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
/// <reference path='ReconnectingWebSocket.ts' />
module openflow {
"use strict";
interface IHashTable<T> {
[key: string]: T;
}
export interface WebAppInterface {
getFirebaseToken(): any;
getOneSignalRegisteredId(): any;
isProductPurchased(): any;
showLoader(): void;
hideLoader(): void;
rateApp(): void;
playSound(file: string): void;
createNotification(displayname: string, message: string): void;
}
export declare var android: WebAppInterface;
type QueuedMessageCallback = (msg: any) => any;
export class QueuedMessage {
constructor(message: any, cb: QueuedMessageCallback) {
this.id = message.id;
this.message = message;
this.cb = cb;
}
public cb: QueuedMessageCallback;
public id: string;
public message: any;
}
export function iosGetOnesignalToken() {
return new Promise<any>(async (resolve, reject) => {
try {
(window as any).bridge.post('onesignaltoken', {}, (results, error) => {
if (error) { return reject(error); }
console.log(results);
resolve(results.token);
});
} catch (error) {
reject(error);
}
});
}
export class WebSocketClient {
private _socketObject: ReconnectingWebSocket = null;
private _url: string = null;
private static instance: WebSocketClient = null;
private _receiveQueue: SocketMessage[] = [];
private _sendQueue: SocketMessage[] = [];
public user: TokenUser = null;
public jwt: string = null;
static $inject = ["$rootScope", "$location", "$window"];
public messageQueue: IHashTable<QueuedMessage> = {};
constructor(public $rootScope: ng.IRootScopeService, public $location, public $window: any) {
this.getJSON("/config", async (error: any, data: any) => {
console.debug("WebSocketClient::onopen: connecting to " + data.wshost);
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;
});
}
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);
});
}
private async onopen(evt: Event): Promise<void> {
console.log("WebSocketClient::onopen: connected");
var me: WebSocketClient = WebSocketClient.instance;
var q: SigninMessage = new SigninMessage();
this.getJSON("/jwt", async (error: any, data: any) => {
try {
if (data === null || data === undefined || (data.jwt === "" && data.rawAssertion === "")) {
if (this.$location.path() !== "/Login") {
console.log("path: " + this.$location.path());
console.log("WebSocketClient::onopen: Not signed in, redirect /Login");
this.$location.path("/Login");
this.$rootScope.$apply();
}
return;
}
var _android: WebAppInterface = null;
try {
_android = android;
} catch (error) {
}
q.jwt = data.jwt;
q.rawAssertion = data.rawAssertion;
q.realm = "browser";
console.log("WebSocketClient::onopen: Validate jwt");
if (_android != null) {
q.realm = "android";
try {
console.debug("getFirebaseToken");
q.firebasetoken = _android.getFirebaseToken();
} catch (error) {
console.log(error);
}
try {
console.debug("getOneSignalRegisteredId");
q.onesignalid = _android.getOneSignalRegisteredId();
} catch (error) {
console.log(error);
}
}
try {
console.debug("iosGetOnesignalToken");
var results = await iosGetOnesignalToken();
q.onesignalid = results.token;
} catch (error) {
console.log(error);
}
console.debug("signing in");
var msg: Message = new Message(); msg.command = "signin"; msg.data = JSON.stringify(q);
var a: any = await this.Send(msg);
var result: SigninMessage = a;
this.user = result.user;
this.$rootScope.$broadcast(msg.command, result);
} catch (error) {
this.user = null;
console.error(error);
this.$location.path("/Login");
this.$rootScope.$apply();
}
});
}
private onclose(evt: CloseEvent): void {
var me: WebSocketClient = WebSocketClient.instance;
}
private onerror(evt: ErrorEvent): void {
var me: WebSocketClient = WebSocketClient.instance;
}
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): Promise<T> {
return new Promise<T>(async (resolve, reject) => {
this._Send(message, ((msg) => {
if (msg.error !== null && msg.error !== undefined) { console.log(message); return reject(msg.error); }
resolve(msg);
}).bind(this));
});
}
private _Send(message: Message, cb: QueuedMessageCallback): void {
var messages: string[] = this.chunkString(message.data, 500);
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);
}
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);
}
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; });
}
});
if (this._socketObject !== null && this._socketObject.readyState !== 1) {
this.connect();
setTimeout(() => {
this.ProcessQueue();
}, 1500);
return;
}
this._sendQueue.forEach(msg => {
try {
if (this._socketObject !== null && this._socketObject.readyState === 1) {
let id: string = msg.id;
this._socketObject.send(JSON.stringify(msg));
this._sendQueue = this._sendQueue.filter(function (msg: SocketMessage): boolean { return msg.id !== id; });
}
} catch (error) {
console.error(error);
}
});
}
}
}