-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.ts
283 lines (234 loc) · 6.21 KB
/
lib.ts
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
/*
Author: Pontus Östlund <https://github.com/poppa>
Permission to copy, modify, and distribute this source for any legal
purpose granted as long as my name is still attached to it. More
specifically, the GPL, LGPL and MPL licenses apply to this software.
*/
'use strict';
export namespace GUID {
export function version4(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
}
/**
* Alias for `console.log` if `[?&]debug=1` is in the query string.
*/
export const wdebug = document.location.search.indexOf('debug=1') > -1 ?
console.log : () => {};
export module WS {
const _session: string = GUID.version4();
export type Message = {
session : string,
data? : any
};
export type Response = {
ok: boolean,
data?: any,
type: number
};
export type SocketAction = 'open'|'error'|'close'|'message';
export type ActionCallback = (ws?: WebSocket, ev?: Event) => any;
export type MessageCallback = (res: Response, ws?: WebSocket, ev?: MessageEvent) => any;
/**
* Factory method for creating a client
*
* @export
* @param {string} url
* @param {(string|string[])} [protos]
* @returns {Client}
*/
export function createClient(url: string, protos?: string|string[]): Client {
return new Client(url, protos);
}
/**
* Factory method for creating a named client
*
* @export
* @param {string} name
* @param {string} url
* @param {(string|string[])} protos
* @returns {NamedClient}
*/
export function createNamedClient(name: string, url: string,
protos?: string|string[]): NamedClient
{
return new NamedClient(url, name, protos);
}
/**
* Basic client
*
* @class Client
*/
export class Client {
/**
* The WebSocket connection object
* @private
* @type {WebSocket}
* @memberOf Client
*/
private sock: WebSocket;
/**
* Are we connected or not
* @private
* @type {boolean}
* @memberOf Client
*/
private _isConnected: boolean = false;
protected onopen: ActionCallback;
protected onclose: ActionCallback;
protected onerror: ActionCallback;
protected onmessage: MessageCallback;
private readResponse(blob: Blob, mev: MessageEvent): void
{
const _ = this;
const blobReader: FileReader = new FileReader();
blobReader.onloadend = function(this: MSBaseReader, ev: ProgressEvent) {
let _res: any = JSON.parse(blobReader.result);
const res: Response = {
ok: _res.ok,
data: _res.data,
type: _res.type
};
_.onmessage(res, _.sock, mev);
};
blobReader.readAsText(blob);
}
/**
* Creates an instance of Client.
* @param {string} _url
* @param {(string|string[])} [_protos]
*
* @memberOf Client
*/
constructor(protected _url: string, protected _protos?: string|string[]) {
}
/**
* Getter for the session ID.
* @readonly
* @type {string}
* @memberOf Client
*/
get session(): string {
return _session;
}
/**
* Check if we're connected or not
* @readonly
* @type {boolean}
* @memberOf Client
*/
get isConnected(): boolean {
return this._isConnected;
}
/**
* Send a message to the server. `msg` will be JSON stringified.
*
* @param {*} msg
*
* @memberOf Client
*/
public send(msg: any): void {
if (this._isConnected) {
this.sock.send(JSON.stringify(msg));
}
}
/**
* Register an action callback. `cb` will be called when an `on[action]`
* event is emitted.
*
* @param {SocketAction} action
* @param {(ActionCallback|MessageCallback)} cb
* @returns {Client}
*
* @memberOf Client
*/
public onaction(action: SocketAction,
cb: ActionCallback|MessageCallback): Client
{
switch (action) {
case 'open':
this.onopen = cb as ActionCallback;
break;
case 'close':
this.onclose = cb as ActionCallback;
break;
case 'error':
this.onclose = cb as ActionCallback;
break;
case 'message':
this.onmessage = cb as MessageCallback;
break;
}
return this;
}
/**
* Start the connection
* @memberOf Client
*/
public start(): void {
const _ = this;
this.sock = new WebSocket(this._url, this._protos);
this.sock.onopen = function onopen(this: WebSocket, ev: Event): any {
wdebug('onopen: ', this, ev);
_._isConnected = true;
if (_.onopen) {
_.onopen(this, ev);
}
};
this.sock.onclose = function onclose(this: WebSocket, ev: Event): any {
wdebug('onclose: ', this, ev);
_._isConnected = false;
if (_.onclose) {
_.onclose(this, ev);
}
};
this.sock.onerror = function onerror(this: WebSocket, ev: Event): any {
wdebug('onerror: ', this, ev);
if (_.onerror) {
_.onerror(this, ev);
}
};
this.sock.onmessage = function onmessage(this: WebSocket, ev: MessageEvent): any {
wdebug('onmessage: ', this, ev);
if (_.onmessage) {
_.readResponse(ev.data, ev);
}
};
}
}
/**
* Named client
*
* @export
* @class NamedClient
* @extends {Client}
*/
export class NamedClient extends Client {
/**
* Creates an instance of NamedClient.
* @param {string} _url
* @param {string} _name
* @param {(string|string[])} [_protos]
*
* @memberOf NamedClient
*/
constructor(protected _url: string, protected _name: string,
protected _protos?: string|string[])
{
super(_url, _protos);
}
/**
* Get the name
*
* @readonly
* @type {string}
* @memberOf NamedClient
*/
get name(): string {
return this._name;
}
}
}