diff --git a/demo/server.js b/demo/server.js index 2a48a21c..856dac9e 100644 --- a/demo/server.js +++ b/demo/server.js @@ -17,7 +17,8 @@ function startServer() { expressWs(app); var terminals = {}, - logs = {}; + unsentOutput = {}, + temporaryDisposable = {}; app.use('/xterm.css', express.static(__dirname + '/../css/xterm.css')); app.get('/logo.png', (req, res) => { @@ -55,9 +56,9 @@ function startServer() { console.log('Created terminal with PID: ' + term.pid); terminals[term.pid] = term; - logs[term.pid] = ''; - term.on('data', function(data) { - logs[term.pid] += data; + unsentOutput[term.pid] = ''; + temporaryDisposable[term.pid] = term.onData(function(data) { + unsentOutput[term.pid] += data; }); res.send(term.pid.toString()); res.end(); @@ -77,7 +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); - ws.send(logs[term.pid]); + temporaryDisposable[term.pid].dispose(); + delete temporaryDisposable[term.pid]; + ws.send(unsentOutput[term.pid]); + // unbuffered delivery after user input let userInput = false; @@ -137,7 +141,7 @@ 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) { + term.onData(function(data) { try { send(data); } catch (ex) { @@ -153,7 +157,8 @@ function startServer() { console.log('Closed terminal ' + term.pid); // Clean things up delete terminals[term.pid]; - delete logs[term.pid]; + delete unsentOutput[term.pid]; + delete temporaryDisposable[term.pid]; }); });