]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/addons/terminado/terminado.js
2 * Implements the attach method that
3 * attaches the terminal to a Terminado 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('../../xterm'));
15 } else if (typeof define
== 'function') {
17 * Require.js is available
19 define(['../../xterm'], attach
);
22 * Plain browser environment
24 attach(window
.Terminal
);
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
.terminadoAttach = 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
) {
69 var data
= JSON
.parse(ev
.data
)
70 if( data
[0] == "stdout" ) {
72 term
._pushToBuffer(data
[1]);
79 term
._sendData = function (data
) {
80 socket
.send(JSON
.stringify(['stdin', data
]));
83 term
._setSize = function (size
) {
84 socket
.send(JSON
.stringify(['set_size', size
.rows
, size
.cols
]));
87 socket
.addEventListener('message', term
._getMessage
);
90 term
.on('data', term
._sendData
);
92 term
.on('resize', term
._setSize
);
94 socket
.addEventListener('close', term
.terminadoDetach
.bind(term
, socket
));
95 socket
.addEventListener('error', term
.terminadoDetach
.bind(term
, socket
));
99 * Detaches the given terminal from the given socket
101 * @param {Xterm} term - The terminal to be detached from the given socket.
102 * @param {WebSocket} socket - The socket from which to detach the current
105 exports
.terminadoDetach = function (term
, socket
) {
106 term
.off('data', term
._sendData
);
108 socket
= (typeof socket
== 'undefined') ? term
.socket
: socket
;
111 socket
.removeEventListener('message', term
._getMessage
);
118 * Attaches the current terminal to the given socket
120 * @param {WebSocket} socket - The socket to attach the current terminal.
121 * @param {boolean} bidirectional - Whether the terminal should send data
122 * to the socket as well.
123 * @param {boolean} buffered - Whether the rendering of incoming data
124 * should happen instantly or at a maximum
125 * frequency of 1 rendering per 10ms.
127 Xterm
.prototype.terminadoAttach = function (socket
, bidirectional
, buffered
) {
128 return exports
.terminadoAttach(this, socket
, bidirectional
, buffered
);
132 * Detaches the current terminal from the given socket.
134 * @param {WebSocket} socket - The socket from which to detach the current
137 Xterm
.prototype.terminadoDetach = function (socket
) {
138 return exports
.terminadoDetach(this, socket
);