]>
Commit | Line | Data |
---|---|---|
8fd350f0 ES |
1 | /* |
2 | * Implements the attach method that | |
3 | * attaches the terminal to a Terminado 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('../../src/xterm')); | |
15 | } else if (typeof define == 'function') { | |
16 | /* | |
17 | * Require.js is available | |
18 | */ | |
19 | define(['../../src/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 | var data = JSON.parse(ev.data) | |
70 | if( data[0] == "stdout" ) { | |
71 | if (buffered) { | |
72 | term._pushToBuffer(data[1]); | |
73 | } else { | |
74 | term.write(data[1]); | |
75 | } | |
76 | } | |
77 | }; | |
78 | ||
79 | term._sendData = function (data) { | |
80 | socket.send(JSON.stringify(['stdin', data])); | |
81 | }; | |
82 | ||
83 | term._setSize = function (size) { | |
84 | socket.send(JSON.stringify(['set_size', size.rows, size.cols])); | |
85 | }; | |
86 | ||
87 | socket.addEventListener('message', term._getMessage); | |
88 | ||
89 | if (bidirectional) { | |
90 | term.on('data', term._sendData); | |
91 | } | |
92 | term.on('resize', term._setSize); | |
93 | ||
94 | socket.addEventListener('close', term.detach.bind(term, socket)); | |
95 | socket.addEventListener('error', term.detach.bind(term, socket)); | |
96 | }; | |
97 | ||
98 | /** | |
99 | * Detaches the given terminal from the given socket | |
100 | * | |
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 | |
103 | * terminal. | |
104 | */ | |
105 | exports.detach = function (term, socket) { | |
106 | term.off('data', term._sendData); | |
107 | ||
108 | socket = (typeof socket == 'undefined') ? term.socket : socket; | |
109 | ||
110 | if (socket) { | |
111 | socket.removeEventListener('message', term._getMessage); | |
112 | } | |
113 | ||
114 | delete term.socket; | |
115 | }; | |
116 | ||
117 | /** | |
118 | * Attaches the current terminal to the given socket | |
119 | * | |
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. | |
126 | */ | |
127 | Xterm.prototype.attach = function (socket, bidirectional, buffered) { | |
128 | return exports.attach(this, socket, bidirectional, buffered); | |
129 | }; | |
130 | ||
131 | /** | |
132 | * Detaches the current terminal from the given socket. | |
133 | * | |
134 | * @param {WebSocket} socket - The socket from which to detach the current | |
135 | * terminal. | |
136 | */ | |
137 | Xterm.prototype.detach = function (socket) { | |
138 | return exports.detach(this, socket); | |
139 | }; | |
140 | ||
141 | return exports; | |
142 | }); |