]>
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) { | |
fff56eea PK |
10 | if (typeof exports === 'object' && typeof module === 'object') { |
11 | /* | |
12 | * CommonJS environment | |
13 | */ | |
441009d3 | 14 | module.exports = attach(require('../../build/xterm')); |
fff56eea PK |
15 | } else if (typeof define == 'function') { |
16 | /* | |
17 | * Require.js is available | |
18 | */ | |
441009d3 | 19 | define(['../../build/xterm'], attach); |
fff56eea PK |
20 | } else { |
21 | /* | |
22 | * Plain browser environment | |
23 | */ | |
ed1a31d1 | 24 | attach(window.Terminal); |
fff56eea | 25 | } |
3f455f90 | 26 | })(function (Xterm) { |
fff56eea PK |
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 | }; | |
bbb68831 | 58 | |
fff56eea PK |
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 | }; | |
bbb68831 | 67 | |
fff56eea PK |
68 | term._getMessage = function (ev) { |
69 | if (buffered) { | |
70 | term._pushToBuffer(ev.data); | |
71 | } else { | |
72 | term.write(ev.data); | |
73 | } | |
6906bf3e | 74 | }; |
932ab00a | 75 | |
fff56eea PK |
76 | term._sendData = function (data) { |
77 | socket.send(data); | |
78 | }; | |
4ce1440f | 79 | |
fff56eea | 80 | socket.addEventListener('message', term._getMessage); |
4ce1440f | 81 | |
fff56eea PK |
82 | if (bidirectional) { |
83 | term.on('data', term._sendData); | |
84 | } | |
6906bf3e | 85 | |
fff56eea PK |
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; | |
bbb68831 | 101 | |
fff56eea PK |
102 | if (socket) { |
103 | socket.removeEventListener('message', term._getMessage); | |
104 | } | |
105 | ||
106 | delete term.socket; | |
107 | }; | |
108 | ||
109 | /** | |
dc67c945 | 110 | * Attaches the current terminal to the given socket |
fff56eea | 111 | * |
dc67c945 PK |
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. | |
fff56eea | 118 | */ |
dc67c945 PK |
119 | Xterm.prototype.attach = function (socket, bidirectional, buffered) { |
120 | return exports.attach(this, socket, bidirectional, buffered); | |
fff56eea PK |
121 | }; |
122 | ||
123 | /** | |
dc67c945 PK |
124 | * Detaches the current terminal from the given socket. |
125 | * | |
126 | * @param {WebSocket} socket - The socket from which to detach the current | |
127 | * terminal. | |
fff56eea | 128 | */ |
dc67c945 PK |
129 | Xterm.prototype.detach = function (socket) { |
130 | return exports.detach(this, socket); | |
131 | }; | |
bbb68831 | 132 | |
fff56eea | 133 | return exports; |
2fce845e | 134 | }); |