]>
git.proxmox.com Git - mirror_xterm.js.git/blob - dist/addons/terminado/terminado.js
2 * This module provides methods for attaching a terminal to a terminado WebSocket stream.
4 * @module xterm/addons/terminado/terminado
9 if (typeof exports
=== 'object' && typeof module
=== 'object') {
11 * CommonJS environment
13 module
.exports
= attach(require('../../xterm'));
14 } else if (typeof define
== 'function') {
16 * Require.js is available
18 define(['../../xterm'], attach
);
21 * Plain browser environment
23 attach(window
.Terminal
);
31 * Attaches the given terminal to the given socket.
33 * @param {Xterm} term - The terminal to be attached to the given socket.
34 * @param {WebSocket} socket - The socket to attach the current terminal.
35 * @param {boolean} bidirectional - Whether the terminal should send data
36 * to the socket as well.
37 * @param {boolean} buffered - Whether the rendering of incoming data
38 * should happen instantly or at a maximum
39 * frequency of 1 rendering per 10ms.
41 exports
.terminadoAttach = function (term
, socket
, bidirectional
, buffered
) {
42 bidirectional
= (typeof bidirectional
== 'undefined') ? true : bidirectional
;
45 term
._flushBuffer = function () {
46 term
.write(term
._attachSocketBuffer
);
47 term
._attachSocketBuffer
= null;
48 clearTimeout(term
._attachSocketBufferTimer
);
49 term
._attachSocketBufferTimer
= null;
52 term
._pushToBuffer = function (data
) {
53 if (term
._attachSocketBuffer
) {
54 term
._attachSocketBuffer
+= data
;
56 term
._attachSocketBuffer
= data
;
57 setTimeout(term
._flushBuffer
, 10);
61 term
._getMessage = function (ev
) {
62 var data
= JSON
.parse(ev
.data
)
63 if( data
[0] == "stdout" ) {
65 term
._pushToBuffer(data
[1]);
72 term
._sendData = function (data
) {
73 socket
.send(JSON
.stringify(['stdin', data
]));
76 term
._setSize = function (size
) {
77 socket
.send(JSON
.stringify(['set_size', size
.rows
, size
.cols
]));
80 socket
.addEventListener('message', term
._getMessage
);
83 term
.on('data', term
._sendData
);
85 term
.on('resize', term
._setSize
);
87 socket
.addEventListener('close', term
.terminadoDetach
.bind(term
, socket
));
88 socket
.addEventListener('error', term
.terminadoDetach
.bind(term
, socket
));
92 * Detaches the given terminal from the given socket
94 * @param {Xterm} term - The terminal to be detached from the given socket.
95 * @param {WebSocket} socket - The socket from which to detach the current
98 exports
.terminadoDetach = function (term
, socket
) {
99 term
.off('data', term
._sendData
);
101 socket
= (typeof socket
== 'undefined') ? term
.socket
: socket
;
104 socket
.removeEventListener('message', term
._getMessage
);
111 * Attaches the current terminal to the given socket
113 * @param {WebSocket} socket - The socket to attach the current terminal.
114 * @param {boolean} bidirectional - Whether the terminal should send data
115 * to the socket as well.
116 * @param {boolean} buffered - Whether the rendering of incoming data
117 * should happen instantly or at a maximum
118 * frequency of 1 rendering per 10ms.
120 Xterm
.prototype.terminadoAttach = function (socket
, bidirectional
, buffered
) {
121 return exports
.terminadoAttach(this, socket
, bidirectional
, buffered
);
125 * Detaches the current terminal from the given socket.
127 * @param {WebSocket} socket - The socket from which to detach the current
130 Xterm
.prototype.terminadoDetach = function (socket
) {
131 return exports
.terminadoDetach(this, socket
);