]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/addons/attach/attach.js
2 * Implements the attach method, that attaches the terminal to a WebSocket stream.
3 * @module xterm/addons/attach/attach
8 if (typeof exports
=== 'object' && typeof module
=== 'object') {
10 * CommonJS environment
12 module
.exports
= attach(require('../../xterm'));
13 } else if (typeof define
== 'function') {
15 * Require.js is available
17 define(['../../xterm'], attach
);
20 * Plain browser environment
22 attach(window
.Terminal
);
30 * Attaches the given terminal to the given socket.
32 * @param {Xterm} term - The terminal to be attached to the given socket.
33 * @param {WebSocket} socket - The socket to attach the current terminal.
34 * @param {boolean} bidirectional - Whether the terminal should send data
35 * to the socket as well.
36 * @param {boolean} buffered - Whether the rendering of incoming data
37 * should happen instantly or at a maximum
38 * frequency of 1 rendering per 10ms.
40 exports
.attach = function (term
, socket
, bidirectional
, buffered
) {
41 bidirectional
= (typeof bidirectional
== 'undefined') ? true : bidirectional
;
44 term
._flushBuffer = function () {
45 term
.write(term
._attachSocketBuffer
);
46 term
._attachSocketBuffer
= null;
47 clearTimeout(term
._attachSocketBufferTimer
);
48 term
._attachSocketBufferTimer
= null;
51 term
._pushToBuffer = function (data
) {
52 if (term
._attachSocketBuffer
) {
53 term
._attachSocketBuffer
+= data
;
55 term
._attachSocketBuffer
= data
;
56 setTimeout(term
._flushBuffer
, 10);
60 term
._getMessage = function (ev
) {
62 term
._pushToBuffer(ev
.data
);
68 term
._sendData = function (data
) {
72 socket
.addEventListener('message', term
._getMessage
);
75 term
.on('data', term
._sendData
);
78 socket
.addEventListener('close', term
.detach
.bind(term
, socket
));
79 socket
.addEventListener('error', term
.detach
.bind(term
, socket
));
83 * Detaches the given terminal from the given socket
85 * @param {Xterm} term - The terminal to be detached from the given socket.
86 * @param {WebSocket} socket - The socket from which to detach the current
89 exports
.detach = function (term
, socket
) {
90 term
.off('data', term
._sendData
);
92 socket
= (typeof socket
== 'undefined') ? term
.socket
: socket
;
95 socket
.removeEventListener('message', term
._getMessage
);
102 * Attaches the current terminal to the given socket
104 * @param {WebSocket} socket - The socket to attach the current terminal.
105 * @param {boolean} bidirectional - Whether the terminal should send data
106 * to the socket as well.
107 * @param {boolean} buffered - Whether the rendering of incoming data
108 * should happen instantly or at a maximum
109 * frequency of 1 rendering per 10ms.
111 Xterm
.prototype.attach = function (socket
, bidirectional
, buffered
) {
112 return exports
.attach(this, socket
, bidirectional
, buffered
);
116 * Detaches the current terminal from the given socket.
118 * @param {WebSocket} socket - The socket from which to detach the current
121 Xterm
.prototype.detach = function (socket
) {
122 return exports
.detach(this, socket
);