1 | ;
|
2 | // *****************************************************************************
|
3 | // Copyright (C) 2018 TypeFox and others.
|
4 | //
|
5 | // This program and the accompanying materials are made available under the
|
6 | // terms of the Eclipse Public License v. 2.0 which is available at
|
7 | // http://www.eclipse.org/legal/epl-2.0.
|
8 | //
|
9 | // This Source Code may also be made available under the following Secondary
|
10 | // Licenses when the conditions for such availability set forth in the Eclipse
|
11 | // Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
12 | // with the GNU Classpath Exception which is available at
|
13 | // https://www.gnu.org/software/classpath/license.html.
|
14 | //
|
15 | // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
16 | // *****************************************************************************
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.WebSocketChannel = void 0;
|
19 | const uint8_array_message_buffer_1 = require("../message-rpc/uint8-array-message-buffer");
|
20 | const channel_1 = require("../message-rpc/channel");
|
21 | const disposable_1 = require("../disposable");
|
22 | /**
|
23 | * A channel that manages the main websocket connection between frontend and backend. All service channels
|
24 | * are reusing this main channel. (multiplexing). An {@link IWebSocket} abstraction is used to keep the implementation
|
25 | * independent of the actual websocket implementation and its execution context (backend vs. frontend).
|
26 | */
|
27 | class WebSocketChannel extends channel_1.AbstractChannel {
|
28 | constructor(socket) {
|
29 | super();
|
30 | this.socket = socket;
|
31 | this.toDispose.push(disposable_1.Disposable.create(() => socket.close()));
|
32 | socket.onClose((reason, code) => this.onCloseEmitter.fire({ reason, code }));
|
33 | socket.onClose(() => this.close());
|
34 | socket.onError(error => this.onErrorEmitter.fire(error));
|
35 | socket.onMessage(data => this.onMessageEmitter.fire(() => {
|
36 | // In the browser context socketIO receives binary messages as ArrayBuffers.
|
37 | // So we have to convert them to a Uint8Array before delegating the message to the read buffer.
|
38 | const buffer = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
|
39 | return new uint8_array_message_buffer_1.Uint8ArrayReadBuffer(buffer);
|
40 | }));
|
41 | }
|
42 | getWriteBuffer() {
|
43 | const result = new uint8_array_message_buffer_1.Uint8ArrayWriteBuffer();
|
44 | result.onCommit(buffer => {
|
45 | if (this.socket.isConnected()) {
|
46 | this.socket.send(buffer);
|
47 | }
|
48 | });
|
49 | return result;
|
50 | }
|
51 | }
|
52 | exports.WebSocketChannel = WebSocketChannel;
|
53 | WebSocketChannel.wsPath = '/services';
|
54 | //# sourceMappingURL=web-socket-channel.js.map |
\ | No newline at end of file |