From 932ab00a53b0e4a093686038c4616bcc46a27dcc Mon Sep 17 00:00:00 2001 From: Paris Date: Tue, 24 Feb 2015 20:55:52 +0200 Subject: [PATCH 1/3] Implement buffered attaching Fixes #21 --- addons/attach/attach.js | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/addons/attach/attach.js b/addons/attach/attach.js index 992c790a..b2cb05c4 100644 --- a/addons/attach/attach.js +++ b/addons/attach/attach.js @@ -1,7 +1,7 @@ /* * Implements the attach method, that * attaches the terminal to a WebSocket stream. - * + * * The bidirectional argument indicates, whether the terminal should * send data to the socket as well and is true, by default. */ @@ -15,20 +15,40 @@ } else { /* * Plain browser environment - */ + */ attach(this.Xterm); } })(function (Xterm) { - Xterm.prototype.attach = function (socket, bidirectional) { + Xterm.prototype.attach = function (socket, bidirectional, buffered) { var term = this; bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional; this.socket = socket; - term._getMessage = function (ev) { - term.write(ev.data); + term._flushBuffer = function () { + term.write(term._attachSocketBuffer); + term._attachSocketBuffer = null; + clearTimeout(term._attachSocketBufferTimer); + term._attachSocketBufferTimer = null; }; - + + term._pushToBuffer = function (data) { + if (term._attachSocketBuffer) { + term._attachSocketBuffer += data; + } else { + term._attachSocketBuffer = data; + setTimeout(term._flushBuffer, 10); + } + }; + + term._getMessage = function (ev) { + if (buffered) { + term._pushToBuffer(ev.data); + } else { + term.write(ev.data); + } + }; + term._sendData = function (data) { socket.send(data); }; @@ -38,11 +58,11 @@ if (bidirectional) { this.on('data', term._sendData); } - + socket.addEventListener('close', term.detach.bind(term, socket)); socket.addEventListener('error', term.detach.bind(term, socket)); }; - + Xterm.prototype.detach = function (socket) { var term = this; From e9d2c806ba1333c323c2a192bf19b2f6c9c49519 Mon Sep 17 00:00:00 2001 From: Paris Date: Tue, 24 Feb 2015 20:57:28 +0200 Subject: [PATCH 2/3] [attach addon] Switch indentation to 4 spaces instead of 2 --- addons/attach/attach.js | 86 ++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/addons/attach/attach.js b/addons/attach/attach.js index b2cb05c4..c12b5135 100644 --- a/addons/attach/attach.js +++ b/addons/attach/attach.js @@ -20,60 +20,60 @@ } })(function (Xterm) { Xterm.prototype.attach = function (socket, bidirectional, buffered) { - var term = this; + var term = this; - bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional; - this.socket = socket; + bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional; + this.socket = socket; - term._flushBuffer = function () { - term.write(term._attachSocketBuffer); - term._attachSocketBuffer = null; - clearTimeout(term._attachSocketBufferTimer); - term._attachSocketBufferTimer = null; - }; + term._flushBuffer = function () { + term.write(term._attachSocketBuffer); + term._attachSocketBuffer = null; + clearTimeout(term._attachSocketBufferTimer); + term._attachSocketBufferTimer = null; + }; - term._pushToBuffer = function (data) { - if (term._attachSocketBuffer) { - term._attachSocketBuffer += data; - } else { - term._attachSocketBuffer = data; - setTimeout(term._flushBuffer, 10); + term._pushToBuffer = function (data) { + if (term._attachSocketBuffer) { + term._attachSocketBuffer += data; + } else { + term._attachSocketBuffer = data; + setTimeout(term._flushBuffer, 10); + } + }; + + term._getMessage = function (ev) { + if (buffered) { + term._pushToBuffer(ev.data); + } else { + term.write(ev.data); + } + }; + + term._sendData = function (data) { + socket.send(data); + }; + + socket.addEventListener('message', term._getMessage); + + if (bidirectional) { + this.on('data', term._sendData); } - }; - term._getMessage = function (ev) { - if (buffered) { - term._pushToBuffer(ev.data); - } else { - term.write(ev.data); - } - }; - - term._sendData = function (data) { - socket.send(data); - }; - - socket.addEventListener('message', term._getMessage); - - if (bidirectional) { - this.on('data', term._sendData); - } - - socket.addEventListener('close', term.detach.bind(term, socket)); - socket.addEventListener('error', term.detach.bind(term, socket)); + socket.addEventListener('close', term.detach.bind(term, socket)); + socket.addEventListener('error', term.detach.bind(term, socket)); }; Xterm.prototype.detach = function (socket) { - var term = this; + var term = this; - term.off('data', term._sendData); + term.off('data', term._sendData); - socket = (typeof socket == 'undefined') ? term.socket : socket; + socket = (typeof socket == 'undefined') ? term.socket : socket; - if (socket) { - socket.removeEventListener('message', term._getMessage); - } + if (socket) { + socket.removeEventListener('message', term._getMessage); + } - delete term.socket; + delete term.socket; }; }); \ No newline at end of file From 13103aecb26ae819741fa22492428bb35ba33365 Mon Sep 17 00:00:00 2001 From: Paris Date: Tue, 24 Feb 2015 22:28:47 +0200 Subject: [PATCH 3/3] [attach addon] Document with JSDoc --- addons/attach/attach.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/addons/attach/attach.js b/addons/attach/attach.js index c12b5135..5d74b17c 100644 --- a/addons/attach/attach.js +++ b/addons/attach/attach.js @@ -19,6 +19,16 @@ attach(this.Xterm); } })(function (Xterm) { + /** + * Attaches the current terminal to the given socket + * + * @param {WebSocket} socket - The socket to attach the current terminal + * @param {boolean} bidirectional - Whether the terminal should send data + * to the socket as well + * @param {boolean} buffered - Whether the rendering of incoming data + * should happen instantly or at a maximum + * frequency of 1 rendering per 10ms + */ Xterm.prototype.attach = function (socket, bidirectional, buffered) { var term = this; @@ -63,6 +73,12 @@ socket.addEventListener('error', term.detach.bind(term, socket)); }; + /** + * Detaches the current terminal from the given socket + * + * @param {WebSocket} socket - The socket from which to detach the current + * terminal + */ Xterm.prototype.detach = function (socket) { var term = this;