Skip to content

Commit 869ce3a

Browse files
authored
Merge pull request #4194 from silamon/fixmissingline
Capture data between terminal creation and connected to terminal in demo
2 parents 16ea621 + cc82f60 commit 869ce3a

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

demo/server.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ function startServer() {
1616
var app = express();
1717
expressWs(app);
1818

19-
var terminals = {};
19+
var terminals = {},
20+
unsentOutput = {},
21+
temporaryDisposable = {};
2022

2123
app.use('/xterm.css', express.static(__dirname + '/../css/xterm.css'));
2224
app.get('/logo.png', (req, res) => {
@@ -54,6 +56,10 @@ function startServer() {
5456

5557
console.log('Created terminal with PID: ' + term.pid);
5658
terminals[term.pid] = term;
59+
unsentOutput[term.pid] = '';
60+
temporaryDisposable[term.pid] = term.onData(function(data) {
61+
unsentOutput[term.pid] += data;
62+
});
5763
res.send(term.pid.toString());
5864
res.end();
5965
});
@@ -72,6 +78,10 @@ function startServer() {
7278
app.ws('/terminals/:pid', function (ws, req) {
7379
var term = terminals[parseInt(req.params.pid)];
7480
console.log('Connected to terminal ' + term.pid);
81+
temporaryDisposable[term.pid].dispose();
82+
delete temporaryDisposable[term.pid];
83+
ws.send(unsentOutput[term.pid]);
84+
delete unsentOutput[term.pid];
7585

7686
// unbuffered delivery after user input
7787
let userInput = false;
@@ -131,8 +141,12 @@ function startServer() {
131141
// WARNING: This is a naive implementation that will not throttle the flow of data. This means
132142
// it could flood the communication channel and make the terminal unresponsive. Learn more about
133143
// the problem and how to implement flow control at https://xtermjs.org/docs/guides/flowcontrol/
134-
term.on('data', function(data) {
135-
send(data);
144+
term.onData(function(data) {
145+
try {
146+
send(data);
147+
} catch (ex) {
148+
// The WebSocket is not open, ignore
149+
}
136150
});
137151
ws.on('message', function(msg) {
138152
term.write(msg);

0 commit comments

Comments
 (0)