Skip to content

Commit

Permalink
Merge pull request #4194 from silamon/fixmissingline
Browse files Browse the repository at this point in the history
Capture data between terminal creation and connected to terminal in demo
  • Loading branch information
Tyriar authored Oct 15, 2022
2 parents 16ea621 + cc82f60 commit 869ce3a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions demo/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function startServer() {
var app = express();
expressWs(app);

var terminals = {};
var terminals = {},
unsentOutput = {},
temporaryDisposable = {};

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

console.log('Created terminal with PID: ' + term.pid);
terminals[term.pid] = term;
unsentOutput[term.pid] = '';
temporaryDisposable[term.pid] = term.onData(function(data) {
unsentOutput[term.pid] += data;
});
res.send(term.pid.toString());
res.end();
});
Expand All @@ -72,6 +78,10 @@ function startServer() {
app.ws('/terminals/:pid', function (ws, req) {
var term = terminals[parseInt(req.params.pid)];
console.log('Connected to terminal ' + term.pid);
temporaryDisposable[term.pid].dispose();
delete temporaryDisposable[term.pid];
ws.send(unsentOutput[term.pid]);
delete unsentOutput[term.pid];

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

0 comments on commit 869ce3a

Please sign in to comment.