]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/addons/attach/attach.js
Check socket state before sending data
[mirror_xterm.js.git] / src / addons / attach / attach.js
CommitLineData
1d300911
PK
1/**
2 * Implements the attach method, that attaches the terminal to a WebSocket stream.
3 * @module xterm/addons/attach/attach
4 * @license MIT
9bd2a1c2 5 */
3f455f90
PK
6
7(function (attach) {
fff56eea
PK
8 if (typeof exports === 'object' && typeof module === 'object') {
9 /*
10 * CommonJS environment
11 */
56ecc77d 12 module.exports = attach(require('../../xterm'));
fff56eea
PK
13 } else if (typeof define == 'function') {
14 /*
15 * Require.js is available
16 */
56ecc77d 17 define(['../../xterm'], attach);
fff56eea
PK
18 } else {
19 /*
20 * Plain browser environment
21 */
ed1a31d1 22 attach(window.Terminal);
fff56eea 23 }
3f455f90 24})(function (Xterm) {
fff56eea
PK
25 'use strict';
26
fff56eea
PK
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 };
bbb68831 50
fff56eea
PK
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 };
bbb68831 59
fff56eea
PK
60 term._getMessage = function (ev) {
61 if (buffered) {
62 term._pushToBuffer(ev.data);
63 } else {
64 term.write(ev.data);
65 }
6906bf3e 66 };
932ab00a 67
fff56eea 68 term._sendData = function (data) {
4a9143ab
JZ
69 if (socket.readyState !== 1) {
70 return;
71 }
72
fff56eea
PK
73 socket.send(data);
74 };
4ce1440f 75
fff56eea 76 socket.addEventListener('message', term._getMessage);
4ce1440f 77
fff56eea
PK
78 if (bidirectional) {
79 term.on('data', term._sendData);
80 }
6906bf3e 81
fff56eea
PK
82 socket.addEventListener('close', term.detach.bind(term, socket));
83 socket.addEventListener('error', term.detach.bind(term, socket));
84 };
85
86 /**
87 * Detaches the given terminal from the given socket
88 *
89 * @param {Xterm} term - The terminal to be detached from the given socket.
90 * @param {WebSocket} socket - The socket from which to detach the current
91 * terminal.
92 */
93 exports.detach = function (term, socket) {
94 term.off('data', term._sendData);
95
96 socket = (typeof socket == 'undefined') ? term.socket : socket;
bbb68831 97
fff56eea
PK
98 if (socket) {
99 socket.removeEventListener('message', term._getMessage);
100 }
101
102 delete term.socket;
103 };
104
105 /**
dc67c945 106 * Attaches the current terminal to the given socket
fff56eea 107 *
dc67c945
PK
108 * @param {WebSocket} socket - The socket to attach the current terminal.
109 * @param {boolean} bidirectional - Whether the terminal should send data
110 * to the socket as well.
111 * @param {boolean} buffered - Whether the rendering of incoming data
112 * should happen instantly or at a maximum
113 * frequency of 1 rendering per 10ms.
fff56eea 114 */
dc67c945
PK
115 Xterm.prototype.attach = function (socket, bidirectional, buffered) {
116 return exports.attach(this, socket, bidirectional, buffered);
fff56eea
PK
117 };
118
119 /**
dc67c945
PK
120 * Detaches the current terminal from the given socket.
121 *
122 * @param {WebSocket} socket - The socket from which to detach the current
123 * terminal.
fff56eea 124 */
dc67c945
PK
125 Xterm.prototype.detach = function (socket) {
126 return exports.detach(this, socket);
127 };
bbb68831 128
fff56eea 129 return exports;
2fce845e 130});