@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
61 lines • 2.77 kB
JavaScript
// *****************************************************************************
// 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-only WITH Classpath-exception-2.0
// *****************************************************************************
import { Uint8ArrayReadBuffer, Uint8ArrayWriteBuffer } from '../message-rpc/uint8-array-message-buffer.js';
import { AbstractChannel } from '../message-rpc/channel.js';
import { Emitter } from 'vscode-languageserver-protocol';
/**
* A channel that manages the main websocket connection between frontend and backend. All service channels
* are reusing this main channel. (multiplexing). An {@link IWebSocket} abstraction is used to keep the implementation
* independent of the actual websocket implementation and its execution context (backend vs. frontend).
*/
export class WebSocketChannel extends AbstractChannel {
static { this.wsPath = '/services'; }
constructor(socket) {
super();
this.socket = socket;
this.onDidConnectEmitter = new Emitter();
this.onDidConnect = this.onDidConnectEmitter.event;
socket.on('connect', () => {
this.onDidConnectEmitter.fire();
});
socket.on('disconnect', reason => {
this.onCloseEmitter.fire({
reason: reason
});
});
socket.on('error', reason => this.onErrorEmitter.fire(reason));
socket.on('message', data => {
// In the browser context socketIO receives binary messages as ArrayBuffers.
// So we have to convert them to a Uint8Array before delegating the message to the read buffer.
const buffer = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
this.onMessageEmitter.fire(() => new Uint8ArrayReadBuffer(buffer));
});
}
getWriteBuffer() {
const result = new Uint8ArrayWriteBuffer();
result.onCommit(buffer => {
if (this.socket.connected) {
this.socket.send(buffer);
}
});
return result;
}
close() {
this.socket.disconnect();
super.close();
}
}
//# sourceMappingURL=web-socket-channel.js.map