/
ws-connection-provider.ts
127 lines (110 loc) · 4.61 KB
/
ws-connection-provider.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
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { injectable, interfaces, decorate, unmanaged } from 'inversify';
import { JsonRpcProxyFactory, JsonRpcProxy, Emitter, Event } from '../../common';
import { WebSocketChannel } from '../../common/messaging/web-socket-channel';
import { Endpoint } from '../endpoint';
import ReconnectingWebSocket from 'reconnecting-websocket';
import { AbstractConnectionProvider } from '../../common/messaging/abstract-connection-provider';
decorate(injectable(), JsonRpcProxyFactory);
decorate(unmanaged(), JsonRpcProxyFactory, 0);
export interface WebSocketOptions {
/**
* True by default.
*/
reconnecting?: boolean;
}
@injectable()
export class WebSocketConnectionProvider extends AbstractConnectionProvider<WebSocketOptions> {
protected readonly onSocketDidOpenEmitter: Emitter<void> = new Emitter();
readonly onSocketDidOpen: Event<void> = this.onSocketDidOpenEmitter.event;
protected readonly onSocketDidCloseEmitter: Emitter<void> = new Emitter();
readonly onSocketDidClose: Event<void> = this.onSocketDidCloseEmitter.event;
static createProxy<T extends object>(container: interfaces.Container, path: string, arg?: object): JsonRpcProxy<T> {
return container.get(WebSocketConnectionProvider).createProxy<T>(path, arg);
}
protected readonly socket: ReconnectingWebSocket;
constructor() {
super();
const url = this.createWebSocketUrl(WebSocketChannel.wsPath);
const socket = this.createWebSocket(url);
socket.onerror = console.error;
socket.onopen = () => {
this.fireSocketDidOpen();
};
socket.onclose = ({ code, reason }) => {
for (const channel of [...this.channels.values()]) {
channel.close(code, reason);
}
this.fireSocketDidClose();
};
socket.onmessage = ({ data }) => {
this.handleIncomingRawMessage(data);
};
this.socket = socket;
window.addEventListener('offline', () => this.tryReconnect());
window.addEventListener('online', () => this.tryReconnect());
}
openChannel(path: string, handler: (channel: WebSocketChannel) => void, options?: WebSocketOptions): void {
if (this.socket.readyState === WebSocket.OPEN) {
super.openChannel(path, handler, options);
} else {
const openChannel = () => {
this.socket.removeEventListener('open', openChannel);
this.openChannel(path, handler, options);
};
this.socket.addEventListener('open', openChannel);
}
}
protected createChannel(id: number): WebSocketChannel {
return new WebSocketChannel(id, content => {
if (this.socket.readyState < WebSocket.CLOSING) {
this.socket.send(content);
}
});
}
/**
* Creates a websocket URL to the current location
*/
protected createWebSocketUrl(path: string): string {
const endpoint = new Endpoint({ path });
return endpoint.getWebSocketUrl().toString();
}
/**
* Creates a web socket for the given url
*/
protected createWebSocket(url: string): ReconnectingWebSocket {
return new ReconnectingWebSocket(url, undefined, {
maxReconnectionDelay: 10000,
minReconnectionDelay: 1000,
reconnectionDelayGrowFactor: 1.3,
connectionTimeout: 10000,
maxRetries: Infinity,
debug: false
});
}
protected fireSocketDidOpen(): void {
this.onSocketDidOpenEmitter.fire(undefined);
}
protected fireSocketDidClose(): void {
this.onSocketDidCloseEmitter.fire(undefined);
}
protected tryReconnect(): void {
if (this.socket.readyState !== WebSocket.CONNECTING) {
this.socket.reconnect();
}
}
}