]> git.proxmox.com Git - mirror_xterm.js.git/blob - addons/attach/attach.js
Move to TypeScript build
[mirror_xterm.js.git] / addons / attach / attach.js
1 /*
2 * Implements the attach method, that
3 * attaches the terminal to a WebSocket stream.
4 *
5 * The bidirectional argument indicates, whether the terminal should
6 * send data to the socket as well and is true, by default.
7 */
8
9 (function (attach) {
10 if (typeof exports === 'object' && typeof module === 'object') {
11 /*
12 * CommonJS environment
13 */
14 module.exports = attach(require('../../build/xterm'));
15 } else if (typeof define == 'function') {
16 /*
17 * Require.js is available
18 */
19 define(['../../build/xterm'], attach);
20 } else {
21 /*
22 * Plain browser environment
23 */
24 attach(window.Terminal);
25 }
26 })(function (Xterm) {
27 'use strict';
28
29 /**
30 * This module provides methods for attaching a terminal to a WebSocket
31 * stream.
32 *
33 * @module xterm/addons/attach/attach
34 */
35 var exports = {};
36
37 /**
38 * Attaches the given terminal to the given socket.
39 *
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.
47 */
48 exports.attach = function (term, socket, bidirectional, buffered) {
49 bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional;
50 term.socket = socket;
51
52 term._flushBuffer = function () {
53 term.write(term._attachSocketBuffer);
54 term._attachSocketBuffer = null;
55 clearTimeout(term._attachSocketBufferTimer);
56 term._attachSocketBufferTimer = null;
57 };
58
59 term._pushToBuffer = function (data) {
60 if (term._attachSocketBuffer) {
61 term._attachSocketBuffer += data;
62 } else {
63 term._attachSocketBuffer = data;
64 setTimeout(term._flushBuffer, 10);
65 }
66 };
67
68 term._getMessage = function (ev) {
69 if (buffered) {
70 term._pushToBuffer(ev.data);
71 } else {
72 term.write(ev.data);
73 }
74 };
75
76 term._sendData = function (data) {
77 socket.send(data);
78 };
79
80 socket.addEventListener('message', term._getMessage);
81
82 if (bidirectional) {
83 term.on('data', term._sendData);
84 }
85
86 socket.addEventListener('close', term.detach.bind(term, socket));
87 socket.addEventListener('error', term.detach.bind(term, socket));
88 };
89
90 /**
91 * Detaches the given terminal from the given socket
92 *
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
95 * terminal.
96 */
97 exports.detach = function (term, socket) {
98 term.off('data', term._sendData);
99
100 socket = (typeof socket == 'undefined') ? term.socket : socket;
101
102 if (socket) {
103 socket.removeEventListener('message', term._getMessage);
104 }
105
106 delete term.socket;
107 };
108
109 /**
110 * Attaches the current terminal to the given socket
111 *
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.
118 */
119 Xterm.prototype.attach = function (socket, bidirectional, buffered) {
120 return exports.attach(this, socket, bidirectional, buffered);
121 };
122
123 /**
124 * Detaches the current terminal from the given socket.
125 *
126 * @param {WebSocket} socket - The socket from which to detach the current
127 * terminal.
128 */
129 Xterm.prototype.detach = function (socket) {
130 return exports.detach(this, socket);
131 };
132
133 return exports;
134 });