Implement buffered attaching

Fixes #21
This commit is contained in:
Paris 2015-02-24 20:55:52 +02:00
parent 6cfd957697
commit 932ab00a53

View File

@ -1,7 +1,7 @@
/* /*
* Implements the attach method, that * Implements the attach method, that
* attaches the terminal to a WebSocket stream. * attaches the terminal to a WebSocket stream.
* *
* The bidirectional argument indicates, whether the terminal should * The bidirectional argument indicates, whether the terminal should
* send data to the socket as well and is true, by default. * send data to the socket as well and is true, by default.
*/ */
@ -15,20 +15,40 @@
} else { } else {
/* /*
* Plain browser environment * Plain browser environment
*/ */
attach(this.Xterm); attach(this.Xterm);
} }
})(function (Xterm) { })(function (Xterm) {
Xterm.prototype.attach = function (socket, bidirectional) { Xterm.prototype.attach = function (socket, bidirectional, buffered) {
var term = this; var term = this;
bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional; bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional;
this.socket = socket; this.socket = socket;
term._getMessage = function (ev) { term._flushBuffer = function () {
term.write(ev.data); 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) { term._sendData = function (data) {
socket.send(data); socket.send(data);
}; };
@ -38,11 +58,11 @@
if (bidirectional) { if (bidirectional) {
this.on('data', term._sendData); this.on('data', term._sendData);
} }
socket.addEventListener('close', term.detach.bind(term, socket)); socket.addEventListener('close', term.detach.bind(term, socket));
socket.addEventListener('error', term.detach.bind(term, socket)); socket.addEventListener('error', term.detach.bind(term, socket));
}; };
Xterm.prototype.detach = function (socket) { Xterm.prototype.detach = function (socket) {
var term = this; var term = this;