]>
Commit | Line | Data |
---|---|---|
9bd2a1c2 PK |
1 | /* |
2 | * Implements the attach method, that | |
3 | * attaches the terminal to a WebSocket stream. | |
932ab00a | 4 | * |
9bd2a1c2 PK |
5 | * The bidirectional argument indicates, whether the terminal should |
6 | * send data to the socket as well and is true, by default. | |
7 | */ | |
3f455f90 PK |
8 | |
9 | (function (attach) { | |
10 | if (typeof define == 'function') { | |
11 | /* | |
12 | * Require.js is available | |
13 | */ | |
14 | define(['../../src/xterm'], attach); | |
15 | } else { | |
16 | /* | |
17 | * Plain browser environment | |
932ab00a | 18 | */ |
3f455f90 PK |
19 | attach(this.Xterm); |
20 | } | |
21 | })(function (Xterm) { | |
bbb68831 PK |
22 | 'use strict'; |
23 | ||
13103aec | 24 | /** |
bbb68831 PK |
25 | * This module provides methods for attaching a terminal to a WebSocket |
26 | * stream. | |
13103aec | 27 | * |
bbb68831 PK |
28 | * @module xterm/addons/attach/attach |
29 | */ | |
30 | var exports = {}; | |
31 | ||
32 | /** | |
33 | * Attaches the given terminal to the given socket. | |
34 | * | |
35 | * @param {Xterm} term - The terminal to be attached to the given socket. | |
36 | * @param {WebSocket} socket - The socket to attach the current terminal. | |
13103aec | 37 | * @param {boolean} bidirectional - Whether the terminal should send data |
bbb68831 | 38 | * to the socket as well. |
13103aec PK |
39 | * @param {boolean} buffered - Whether the rendering of incoming data |
40 | * should happen instantly or at a maximum | |
bbb68831 | 41 | * frequency of 1 rendering per 10ms. |
13103aec | 42 | */ |
2fce845e | 43 | exports.attach = function (term, socket, bidirectional, buffered) { |
e9d2c806 | 44 | bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional; |
bbb68831 | 45 | term.socket = socket; |
3f455f90 | 46 | |
e9d2c806 PK |
47 | term._flushBuffer = function () { |
48 | term.write(term._attachSocketBuffer); | |
49 | term._attachSocketBuffer = null; | |
50 | clearTimeout(term._attachSocketBufferTimer); | |
51 | term._attachSocketBufferTimer = null; | |
52 | }; | |
932ab00a | 53 | |
e9d2c806 PK |
54 | term._pushToBuffer = function (data) { |
55 | if (term._attachSocketBuffer) { | |
56 | term._attachSocketBuffer += data; | |
57 | } else { | |
58 | term._attachSocketBuffer = data; | |
59 | setTimeout(term._flushBuffer, 10); | |
60 | } | |
61 | }; | |
932ab00a | 62 | |
e9d2c806 PK |
63 | term._getMessage = function (ev) { |
64 | if (buffered) { | |
65 | term._pushToBuffer(ev.data); | |
66 | } else { | |
67 | term.write(ev.data); | |
68 | } | |
69 | }; | |
932ab00a | 70 | |
e9d2c806 PK |
71 | term._sendData = function (data) { |
72 | socket.send(data); | |
73 | }; | |
74f95261 | 74 | |
e9d2c806 | 75 | socket.addEventListener('message', term._getMessage); |
3f455f90 | 76 | |
e9d2c806 | 77 | if (bidirectional) { |
2fce845e | 78 | term.on('data', term._sendData); |
e9d2c806 | 79 | } |
932ab00a | 80 | |
e9d2c806 PK |
81 | socket.addEventListener('close', term.detach.bind(term, socket)); |
82 | socket.addEventListener('error', term.detach.bind(term, socket)); | |
6906bf3e | 83 | }; |
932ab00a | 84 | |
13103aec | 85 | /** |
bbb68831 | 86 | * Detaches the given terminal from the given socket |
13103aec | 87 | * |
bbb68831 | 88 | * @param {Xterm} term - The terminal to be detached from the given socket. |
13103aec | 89 | * @param {WebSocket} socket - The socket from which to detach the current |
bbb68831 | 90 | * terminal. |
13103aec | 91 | */ |
bbb68831 | 92 | exports.detach = function (term, socket) { |
e9d2c806 | 93 | term.off('data', term._sendData); |
4ce1440f | 94 | |
e9d2c806 | 95 | socket = (typeof socket == 'undefined') ? term.socket : socket; |
4ce1440f | 96 | |
e9d2c806 PK |
97 | if (socket) { |
98 | socket.removeEventListener('message', term._getMessage); | |
99 | } | |
6906bf3e | 100 | |
e9d2c806 | 101 | delete term.socket; |
3f455f90 | 102 | }; |
bbb68831 PK |
103 | |
104 | /** | |
105 | * Attaches the current terminal to the given socket | |
106 | * | |
107 | * @param {WebSocket} socket - The socket to attach the current terminal. | |
108 | * @param {boolean} bidirectional - Whether the terminal should send data | |
109 | * to the socket as well. | |
110 | * @param {boolean} buffered - Whether the rendering of incoming data | |
111 | * should happen instantly or at a maximum | |
112 | * frequency of 1 rendering per 10ms. | |
113 | */ | |
114 | Xterm.prototype.attach = function (socket, bidirectional, buffered) { | |
115 | return exports.attach(this, socket, bidirectional, buffered); | |
116 | }; | |
117 | ||
118 | /** | |
119 | * Detaches the current terminal from the given socket. | |
120 | * | |
121 | * @param {WebSocket} socket - The socket from which to detach the current | |
122 | * terminal. | |
123 | */ | |
124 | Xterm.prototype.detach = function (socket) { | |
125 | return exports.detach(this, socket); | |
126 | }; | |
127 | ||
128 | return exports; | |
2fce845e | 129 | }); |