-
Notifications
You must be signed in to change notification settings - Fork 3k
/
WebSocketSubject.ts
280 lines (256 loc) · 8.02 KB
/
WebSocketSubject.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
import { Subject, AnonymousSubject } from '../../Subject';
import { Subscriber } from '../../Subscriber';
import { Observable } from '../../Observable';
import { Subscription } from '../../Subscription';
import { Operator } from '../../Operator';
import { root } from '../../util/root';
import { ReplaySubject } from '../../ReplaySubject';
import { Observer, NextObserver } from '../../Observer';
import { tryCatch } from '../../util/tryCatch';
import { errorObject } from '../../util/errorObject';
import { assign } from '../../util/assign';
export interface WebSocketSubjectConfig {
url: string;
protocol?: string | Array<string>;
resultSelector?: <T>(e: MessageEvent) => T;
openObserver?: NextObserver<Event>;
closeObserver?: NextObserver<CloseEvent>;
closingObserver?: NextObserver<void>;
WebSocketCtor?: { new(url: string, protocol?: string|Array<string>): WebSocket };
binaryType?: 'blob' | 'arraybuffer';
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @extends {Ignored}
* @hide true
*/
export class WebSocketSubject<T> extends AnonymousSubject<T> {
url: string;
protocol: string|Array<string>;
socket: WebSocket;
openObserver: NextObserver<Event>;
closeObserver: NextObserver<CloseEvent>;
closingObserver: NextObserver<void>;
WebSocketCtor: { new(url: string, protocol?: string|Array<string>): WebSocket };
binaryType?: 'blob' | 'arraybuffer';
private _output: Subject<T>;
resultSelector(e: MessageEvent) {
return JSON.parse(e.data);
}
/**
* Wrapper around the w3c-compatible WebSocket object provided by the browser.
*
* @example <caption>Wraps browser WebSocket</caption>
*
* let socket$ = Observable.webSocket('ws://localhost:8081');
*
* socket$.subscribe(
* (msg) => console.log('message received: ' + msg),
* (err) => console.log(err),
* () => console.log('complete')
* );
*
* socket$.next(JSON.stringify({ op: 'hello' }));
*
* @example <caption>Wraps WebSocket from nodejs-websocket (using node.js)</caption>
*
* import { w3cwebsocket } from 'websocket';
*
* let socket$ = Observable.webSocket({
* url: 'ws://localhost:8081',
* WebSocketCtor: w3cwebsocket
* });
*
* socket$.subscribe(
* (msg) => console.log('message received: ' + msg),
* (err) => console.log(err),
* () => console.log('complete')
* );
*
* socket$.next(JSON.stringify({ op: 'hello' }));
*
* @param {string | WebSocketSubjectConfig} urlConfigOrSource the source of the websocket as an url or a structure defining the websocket object
* @return {WebSocketSubject}
* @static true
* @name webSocket
* @owner Observable
*/
static create<T>(urlConfigOrSource: string | WebSocketSubjectConfig): WebSocketSubject<T> {
return new WebSocketSubject<T>(urlConfigOrSource);
}
constructor(urlConfigOrSource: string | WebSocketSubjectConfig | Observable<T>, destination?: Observer<T>) {
if (urlConfigOrSource instanceof Observable) {
super(destination, <Observable<T>> urlConfigOrSource);
} else {
super();
this.WebSocketCtor = root.WebSocket;
this._output = new Subject<T>();
if (typeof urlConfigOrSource === 'string') {
this.url = urlConfigOrSource;
} else {
// WARNING: config object could override important members here.
assign(this, urlConfigOrSource);
}
if (!this.WebSocketCtor) {
throw new Error('no WebSocket constructor can be found');
}
this.destination = new ReplaySubject();
}
}
lift<R>(operator: Operator<T, R>): WebSocketSubject<R> {
const sock = new WebSocketSubject<R>(this, <any> this.destination);
sock.operator = operator;
return sock;
}
private _resetState() {
this.socket = null;
if (!this.source) {
this.destination = new ReplaySubject();
}
this._output = new Subject<T>();
}
// TODO: factor this out to be a proper Operator/Subscriber implementation and eliminate closures
multiplex(subMsg: () => any, unsubMsg: () => any, messageFilter: (value: T) => boolean) {
const self = this;
return new Observable((observer: Observer<any>) => {
const result = tryCatch(subMsg)();
if (result === errorObject) {
observer.error(errorObject.e);
} else {
self.next(result);
}
let subscription = self.subscribe(x => {
const result = tryCatch(messageFilter)(x);
if (result === errorObject) {
observer.error(errorObject.e);
} else if (result) {
observer.next(x);
}
},
err => observer.error(err),
() => observer.complete());
return () => {
const result = tryCatch(unsubMsg)();
if (result === errorObject) {
observer.error(errorObject.e);
} else {
self.next(result);
}
subscription.unsubscribe();
};
});
}
private _connectSocket() {
const { WebSocketCtor } = this;
const observer = this._output;
let socket: WebSocket = null;
try {
socket = this.protocol ?
new WebSocketCtor(this.url, this.protocol) :
new WebSocketCtor(this.url);
this.socket = socket;
if (this.binaryType) {
this.socket.binaryType = this.binaryType;
}
} catch (e) {
observer.error(e);
return;
}
const subscription = new Subscription(() => {
this.socket = null;
if (socket && socket.readyState === 1) {
socket.close();
}
});
socket.onopen = (e: Event) => {
const openObserver = this.openObserver;
if (openObserver) {
openObserver.next(e);
}
const queue = this.destination;
this.destination = Subscriber.create(
(x) => socket.readyState === 1 && socket.send(x),
(e) => {
const closingObserver = this.closingObserver;
if (closingObserver) {
closingObserver.next(undefined);
}
if (e && e.code) {
socket.close(e.code, e.reason);
} else {
observer.error(new TypeError('WebSocketSubject.error must be called with an object with an error code, ' +
'and an optional reason: { code: number, reason: string }'));
}
this._resetState();
},
( ) => {
const closingObserver = this.closingObserver;
if (closingObserver) {
closingObserver.next(undefined);
}
socket.close();
this._resetState();
}
);
if (queue && queue instanceof ReplaySubject) {
subscription.add((<ReplaySubject<T>>queue).subscribe(this.destination));
}
};
socket.onerror = (e: Event) => {
this._resetState();
observer.error(e);
};
socket.onclose = (e: CloseEvent) => {
this._resetState();
const closeObserver = this.closeObserver;
if (closeObserver) {
closeObserver.next(e);
}
if (e.wasClean) {
observer.complete();
} else {
observer.error(e);
}
};
socket.onmessage = (e: MessageEvent) => {
const result = tryCatch(this.resultSelector)(e);
if (result === errorObject) {
observer.error(errorObject.e);
} else {
observer.next(result);
}
};
}
protected _subscribe(subscriber: Subscriber<T>): Subscription {
const { source } = this;
if (source) {
return source.subscribe(subscriber);
}
if (!this.socket) {
this._connectSocket();
}
let subscription = new Subscription();
subscription.add(this._output.subscribe(subscriber));
subscription.add(() => {
const { socket } = this;
if (this._output.observers.length === 0) {
if (socket && socket.readyState === 1) {
socket.close();
}
this._resetState();
}
});
return subscription;
}
unsubscribe() {
const { source, socket } = this;
if (socket && socket.readyState === 1) {
socket.close();
this._resetState();
}
super.unsubscribe();
if (!source) {
this.destination = new ReplaySubject();
}
}
}