Skip to content

Commit

Permalink
ready option in attach addon v3 and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jerch committed May 14, 2019
1 parent 9cb6706 commit 7536eab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions demo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ function runRealTerminal(): void {
* To run it with UTF8 binary transport, swap comment on
* the lines below. (Must also be switched in server.js)
*/
term.loadAddon(new AttachAddon(socket));
// term.loadAddon(new AttachAddon(socket, {inputUtf8: true}));
term.loadAddon(new AttachAddon(socket, {sendReady: true}));
// term.loadAddon(new AttachAddon(socket, {sendReady: true, inputUtf8: true}));

term._initialized = true;
}
Expand Down
14 changes: 6 additions & 8 deletions demo/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,13 @@ function startServer() {
}
const send = USE_BINARY_UTF8 ? bufferUtf8(ws, 5) : buffer(ws, 5);

term.on('data', function(data) {
try {
send(data);
} catch (ex) {
// The WebSocket is not open, ignore
ws.once('message', msg => {
// wait once for READY message at the beginning
if (msg !== '#READY#') {
throw new Error(`excepted "#READY#" as first message, but got "${msg}"`);
}
});
ws.on('message', function(msg) {
term.write(msg);
ws.on('message', msg => term.write(msg));
term.on('data', data => send(data));
});
ws.on('close', function () {
term.kill();
Expand Down
20 changes: 17 additions & 3 deletions src/addons/attach/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import { Terminal, IDisposable } from 'xterm';
import { IAttachAddonTerminal } from './Interfaces';

const READY_MSG = '#READY#';

/**
* Attaches the given terminal to the given socket.
*
Expand All @@ -16,8 +18,9 @@ import { IAttachAddonTerminal } from './Interfaces';
* @param bidirectional Whether the terminal should send data to the socket as well.
* @param buffered Whether the rendering of incoming data should happen instantly or at a maximum
* frequency of 1 rendering per 10ms.
* @param buffered Whether the addon should send '#READY#' on startup
*/
export function attach(term: Terminal, socket: WebSocket, bidirectional: boolean, buffered: boolean): void {
export function attach(term: Terminal, socket: WebSocket, bidirectional: boolean, buffered: boolean, sendReady: boolean): void {
const addonTerminal = <IAttachAddonTerminal>term;
bidirectional = (typeof bidirectional === 'undefined') ? true : bidirectional;
addonTerminal.__socket = socket;
Expand Down Expand Up @@ -96,6 +99,16 @@ export function attach(term: Terminal, socket: WebSocket, bidirectional: boolean

addonTerminal._core.register(addSocketListener(socket, 'close', () => detach(addonTerminal, socket)));
addonTerminal._core.register(addSocketListener(socket, 'error', () => detach(addonTerminal, socket)));

if (sendReady) {
if (this._socket.readyState === 1) {
// connection already opened, send right away
socket.send(READY_MSG);
} else if (this._socket.readyState === 0) {
// still connecting, thus wait for open event
addSocketListener(socket, 'open', () => socket.send(READY_MSG));
}
}
}

function addSocketListener(socket: WebSocket, type: string, handler: (this: WebSocket, ev: Event) => any): IDisposable {
Expand Down Expand Up @@ -141,9 +154,10 @@ export function apply(terminalConstructor: typeof Terminal): void {
* @param bidirectional Whether the terminal should send data to the socket as well.
* @param buffered Whether the rendering of incoming data should happen instantly or at a maximum
* frequency of 1 rendering per 10ms.
* @param buffered Whether the addon should send '#READY#' on startup
*/
(<any>terminalConstructor.prototype).attach = function (socket: WebSocket, bidirectional: boolean, buffered: boolean): void {
attach(this, socket, bidirectional, buffered);
(<any>terminalConstructor.prototype).attach = function (socket: WebSocket, bidirectional: boolean, buffered: boolean, sendReady: boolean): void {
attach(this, socket, bidirectional, buffered, sendReady);
};

/**
Expand Down

0 comments on commit 7536eab

Please sign in to comment.