]>
git.proxmox.com Git - mirror_xterm.js.git/blob - addons/attach/attach.js
2 * Implements the attach method, that
3 * attaches the terminal to a WebSocket stream.
5 * The bidirectional argument indicates, whether the terminal should
6 * send data to the socket as well and is true, by default.
10 if (typeof exports
=== 'object' && typeof module
=== 'object') {
12 * CommonJS environment
14 module
.exports
= attach(require('../../src/xterm'));
15 } else if (typeof define
== 'function') {
17 * Require.js is available
19 define(['../../src/xterm'], attach
);
22 * Plain browser environment
30 * This module provides methods for attaching a terminal to a WebSocket
33 * @module xterm/addons/attach/attach
38 * Attaches the given terminal to the given socket.
40 * @param {Xterm} term - The terminal to be attached to the given socket.
41 * @param {WebSocket} socket - The socket to attach the current terminal.
42 * @param {boolean} bidirectional - Whether the terminal should send data
43 * to the socket as well.
44 * @param {boolean} buffered - Whether the rendering of incoming data
45 * should happen instantly or at a maximum
46 * frequency of 1 rendering per 10ms.
48 exports
.attach = function (term
, socket
, bidirectional
, buffered
) {
49 bidirectional
= (typeof bidirectional
== 'undefined') ? true : bidirectional
;
52 term
._flushBuffer = function () {
53 term
.write(term
._attachSocketBuffer
);
54 term
._attachSocketBuffer
= null;
55 clearTimeout(term
._attachSocketBufferTimer
);
56 term
._attachSocketBufferTimer
= null;
59 term
._pushToBuffer = function (data
) {
60 if (term
._attachSocketBuffer
) {
61 term
._attachSocketBuffer
+= data
;
63 term
._attachSocketBuffer
= data
;
64 setTimeout(term
._flushBuffer
, 10);
68 term
._getMessage = function (ev
) {
70 term
._pushToBuffer(ev
.data
);
76 term
._sendData = function (data
) {
80 socket
.addEventListener('message', term
._getMessage
);
83 term
.on('data', term
._sendData
);
86 socket
.addEventListener('close', term
.detach
.bind(term
, socket
));
87 socket
.addEventListener('error', term
.detach
.bind(term
, socket
));
91 * Detaches the given terminal from the given socket
93 * @param {Xterm} term - The terminal to be detached from the given socket.
94 * @param {WebSocket} socket - The socket from which to detach the current
97 exports
.detach = function (term
, socket
) {
98 term
.off('data', term
._sendData
);
100 socket
= (typeof socket
== 'undefined') ? term
.socket
: socket
;
103 socket
.removeEventListener('message', term
._getMessage
);
110 * Attaches the current terminal to the given socket
112 * @param {WebSocket} socket - The socket to attach the current terminal.
113 * @param {boolean} bidirectional - Whether the terminal should send data
114 * to the socket as well.
115 * @param {boolean} buffered - Whether the rendering of incoming data
116 * should happen instantly or at a maximum
117 * frequency of 1 rendering per 10ms.
119 Xterm
.prototype.attach = function (socket
, bidirectional
, buffered
) {
120 return exports
.attach(this, socket
, bidirectional
, buffered
);
124 * Detaches the current terminal from the given socket.
126 * @param {WebSocket} socket - The socket from which to detach the current
129 Xterm
.prototype.detach = function (socket
) {
130 return exports
.detach(this, socket
);