]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/addons/attach/attach.js
Update documentation with cleaner license declaration
[mirror_xterm.js.git] / src / addons / attach / attach.js
1 /**
2 * Implements the attach method, that attaches the terminal to a WebSocket stream.
3 * @module xterm/addons/attach/attach
4 * @license MIT
5 */
6
7 (function (attach) {
8 if (typeof exports === 'object' && typeof module === 'object') {
9 /*
10 * CommonJS environment
11 */
12 module.exports = attach(require('../../xterm'));
13 } else if (typeof define == 'function') {
14 /*
15 * Require.js is available
16 */
17 define(['../../xterm'], attach);
18 } else {
19 /*
20 * Plain browser environment
21 */
22 attach(window.Terminal);
23 }
24 })(function (Xterm) {
25 'use strict';
26
27 var exports = {};
28
29 /**
30 * Attaches the given terminal to the given socket.
31 *
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.
39 */
40 exports.attach = function (term, socket, bidirectional, buffered) {
41 bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional;
42 term.socket = socket;
43
44 term._flushBuffer = function () {
45 term.write(term._attachSocketBuffer);
46 term._attachSocketBuffer = null;
47 clearTimeout(term._attachSocketBufferTimer);
48 term._attachSocketBufferTimer = null;
49 };
50
51 term._pushToBuffer = function (data) {
52 if (term._attachSocketBuffer) {
53 term._attachSocketBuffer += data;
54 } else {
55 term._attachSocketBuffer = data;
56 setTimeout(term._flushBuffer, 10);
57 }
58 };
59
60 term._getMessage = function (ev) {
61 if (buffered) {
62 term._pushToBuffer(ev.data);
63 } else {
64 term.write(ev.data);
65 }
66 };
67
68 term._sendData = function (data) {
69 socket.send(data);
70 };
71
72 socket.addEventListener('message', term._getMessage);
73
74 if (bidirectional) {
75 term.on('data', term._sendData);
76 }
77
78 socket.addEventListener('close', term.detach.bind(term, socket));
79 socket.addEventListener('error', term.detach.bind(term, socket));
80 };
81
82 /**
83 * Detaches the given terminal from the given socket
84 *
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
87 * terminal.
88 */
89 exports.detach = function (term, socket) {
90 term.off('data', term._sendData);
91
92 socket = (typeof socket == 'undefined') ? term.socket : socket;
93
94 if (socket) {
95 socket.removeEventListener('message', term._getMessage);
96 }
97
98 delete term.socket;
99 };
100
101 /**
102 * Attaches the current terminal to the given socket
103 *
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.
110 */
111 Xterm.prototype.attach = function (socket, bidirectional, buffered) {
112 return exports.attach(this, socket, bidirectional, buffered);
113 };
114
115 /**
116 * Detaches the current terminal from the given socket.
117 *
118 * @param {WebSocket} socket - The socket from which to detach the current
119 * terminal.
120 */
121 Xterm.prototype.detach = function (socket) {
122 return exports.detach(this, socket);
123 };
124
125 return exports;
126 });