/******************************************************************************** * 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 { decorate, injectable, inject, interfaces, unmanaged, optional } from 'inversify'; import { ConsoleLogger, createWebSocketConnection } from 'vscode-ws-jsonrpc'; import { Emitter, Event } from '@gedit/utils'; import { ConnectionHandler, JsonRpcProxy, JsonRpcProxyFactory, IConnectionProvider } from '../common'; import { WebSocketChannel } from '../common/messaging/web-socket-channel'; import { Endpoint } from '@gedit/utils/lib/browser'; import ReconnectingWebSocket from 'reconnecting-websocket'; decorate(injectable(), JsonRpcProxyFactory); decorate(unmanaged(), JsonRpcProxyFactory, 0); export const WebSocketOptions = Symbol('WebSocketOptions'); export interface WebSocketOptions { /** * True by default. */ reconnecting?: boolean; /** * Location for end point */ location?: Endpoint.Location } @injectable() export class WebSocketConnectionProvider implements IConnectionProvider { protected channelIdSeq = 0; protected readonly socket: ReconnectingWebSocket; protected readonly channels = new Map(); protected readonly onIncomingMessageActivityEmitter: Emitter = new Emitter(); public onIncomingMessageActivity: Event = this.onIncomingMessageActivityEmitter.event; constructor( @inject(WebSocketOptions) @optional() protected readonly defaultOptions?: WebSocketOptions ) { const url = this.createWebSocketUrl(WebSocketChannel.wsPath); const socket = this.createWebSocket(url); socket.onerror = console.error; socket.onclose = ({code, reason}) => { for (const channel of [...this.channels.values()]) { channel.close(code, reason); } }; socket.onmessage = ({data}) => { const message: WebSocketChannel.Message = JSON.parse(data); const channel = this.channels.get(message.id); if (channel) { channel.handleMessage(message); } else { console.error('The ws channel does not exist', message.id); } this.onIncomingMessageActivityEmitter.fire(undefined); }; this.socket = socket; } /** * Create a proxy object to remote interface of T type * over a web socket connection for the given path and proxy factory. */ static createProxy(container: interfaces.Container, path: string, factory: JsonRpcProxyFactory): JsonRpcProxy; /** * Create a proxy object to remote interface of T type * over a web socket connection for the given path. * * An optional target can be provided to handle * notifications and requests from a remote side. */ static createProxy(container: interfaces.Container, path: string, target?: object): JsonRpcProxy; static createProxy(container: interfaces.Container, path: string, arg?: object): JsonRpcProxy { return container.get(WebSocketConnectionProvider).createProxy(path, arg); } /** * Create a proxy object to remote interface of T type * over a web socket connection for the given path and proxy factory. */ createProxy(path: string, factory: JsonRpcProxyFactory): JsonRpcProxy; /** * Create a proxy object to remote interface of T type * over a web socket connection for the given path. * * An optional target can be provided to handle * notifications and requests from a remote side. */ createProxy(path: string, target?: object): JsonRpcProxy; createProxy(path: string, arg?: object): JsonRpcProxy { const factory = arg instanceof JsonRpcProxyFactory ? arg : new JsonRpcProxyFactory(arg); this.listen({ path, onConnection: c => factory.listen(c) }); return factory.createProxy(); } /** * Install a connection handler for the given path. */ listen(handler: ConnectionHandler, options?: WebSocketOptions): void { this.openChannel(handler.path, channel => { const connection = createWebSocketConnection(channel, this.createLogger()); connection.onDispose(() => channel.close()); handler.onConnection(connection); }, options); } openChannel(path: string, handler: (channel: WebSocketChannel) => void, options: WebSocketOptions | undefined = this.defaultOptions): void { if (this.socket.readyState === WebSocket.OPEN) { this.doOpenChannel(path, handler, options); } else { const openChannel = () => { this.socket.removeEventListener('open', openChannel); this.openChannel(path, handler, options); }; this.socket.addEventListener('open', openChannel); } } protected doOpenChannel(path: string, handler: (channel: WebSocketChannel) => void, options?: WebSocketOptions): void { const id = this.channelIdSeq++; const channel = this.createChannel(id); this.channels.set(id, channel); channel.onClose(() => { if (this.channels.delete(channel.id)) { const {reconnecting} = {reconnecting: true, ...options}; if (reconnecting) { this.openChannel(path, handler, options); } } else { console.error('The ws channel does not exist', channel.id); } }); channel.onOpen(() => handler(channel)); channel.open(path); } protected createChannel(id: number): WebSocketChannel { return new WebSocketChannel(id, content => { if (this.socket.readyState < WebSocket.CLOSING) { this.socket.send(content); } }); } protected createLogger(): ConsoleLogger { return new ConsoleLogger(); } /** * Creates a websocket URL to the current location */ protected createWebSocketUrl(path: string): string { const endpoint = new Endpoint({path}, this.defaultOptions?.location); 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 }); } }