UNPKG

open-collaboration-protocol

Version:

Open Collaboration Protocol implementation, part of the Open Collaboration Tools project

68 lines 2.44 kB
// ****************************************************************************** // Copyright 2024 TypeFox GmbH // This program and the accompanying materials are made available under the // terms of the MIT License, which is available in the project root. // ****************************************************************************** import { Emitter } from '../utils/event.js'; import { Deferred } from '../utils/promise.js'; import { io } from 'socket.io-client'; export const SocketIoTransportProvider = { id: 'socket.io', createTransport: (url, headers) => { const socket = io(url, { extraHeaders: headers }); const transport = new SocketIoTransport(socket); return transport; } }; export class SocketIoTransport { socket; id = 'socket.io'; onReconnectEmitter = new Emitter(); onDisconnectEmitter = new Emitter(); onErrorEmitter = new Emitter(); disconnectTimeout; ready = new Deferred(); get onDisconnect() { return this.onDisconnectEmitter.event; } get onReconnect() { return this.onReconnectEmitter.event; } get onError() { return this.onErrorEmitter.event; } constructor(socket) { this.socket = socket; this.socket.on('disconnect', (_reason, _description) => { this.ready = new Deferred(); // Give it 30 seconds to reconnect before firing the disconnect event this.disconnectTimeout = setTimeout(() => { this.onDisconnectEmitter.fire(); this.disconnectTimeout = undefined; }, 30_000); }); this.socket.io.on('reconnect', () => { if (this.disconnectTimeout) { clearTimeout(this.disconnectTimeout); this.disconnectTimeout = undefined; this.ready.resolve(); } this.onReconnectEmitter.fire(); }); this.socket.on('error', () => this.onErrorEmitter.fire('Websocket connection closed unexpectedly.')); this.socket.on('connect', () => this.ready.resolve()); } async write(data) { await this.ready.promise.then(() => this.socket.send(data)); } read(cb) { this.socket.on('message', data => cb(data)); } dispose() { this.onDisconnectEmitter.dispose(); this.socket.close(); } } //# sourceMappingURL=socket-io-transport.js.map