From fb91b01217d6b75102b9bdf4a9fed7e40d7cc633 Mon Sep 17 00:00:00 2001 From: Simon Lamon Date: Sun, 9 Oct 2022 12:16:26 +0000 Subject: [PATCH 1/3] Adds logs again in server.js --- demo/server.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/demo/server.js b/demo/server.js index 0e82f9e96f..2a48a21c58 100644 --- a/demo/server.js +++ b/demo/server.js @@ -16,7 +16,8 @@ function startServer() { var app = express(); expressWs(app); - var terminals = {}; + var terminals = {}, + logs = {}; app.use('/xterm.css', express.static(__dirname + '/../css/xterm.css')); app.get('/logo.png', (req, res) => { @@ -54,6 +55,10 @@ 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; + }); res.send(term.pid.toString()); res.end(); }); @@ -72,6 +77,7 @@ 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]); // unbuffered delivery after user input let userInput = false; @@ -132,7 +138,11 @@ function startServer() { // 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); + try { + send(data); + } catch (ex) { + // The WebSocket is not open, ignore + } }); ws.on('message', function(msg) { term.write(msg); @@ -143,6 +153,7 @@ function startServer() { console.log('Closed terminal ' + term.pid); // Clean things up delete terminals[term.pid]; + delete logs[term.pid]; }); }); From a055948937e2260a59d96d436463e16e9f223479 Mon Sep 17 00:00:00 2001 From: Simon Lamon Date: Sat, 15 Oct 2022 10:45:23 +0000 Subject: [PATCH 2/3] unsentOutput instead of logs and dispose temporary listener --- demo/server.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/demo/server.js b/demo/server.js index 2a48a21c58..856dac9e12 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]; }); }); From cc82f605fe073fa9f7b0924e4fddeb37a16bd795 Mon Sep 17 00:00:00 2001 From: Simon Lamon Date: Sat, 15 Oct 2022 10:47:59 +0000 Subject: [PATCH 3/3] delete clean up --- demo/server.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/demo/server.js b/demo/server.js index 856dac9e12..03689f20a0 100644 --- a/demo/server.js +++ b/demo/server.js @@ -17,8 +17,8 @@ function startServer() { expressWs(app); var terminals = {}, - unsentOutput = {}, - temporaryDisposable = {}; + unsentOutput = {}, + temporaryDisposable = {}; app.use('/xterm.css', express.static(__dirname + '/../css/xterm.css')); app.get('/logo.png', (req, res) => { @@ -81,7 +81,7 @@ function startServer() { 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; @@ -157,8 +157,6 @@ function startServer() { console.log('Closed terminal ' + term.pid); // Clean things up delete terminals[term.pid]; - delete unsentOutput[term.pid]; - delete temporaryDisposable[term.pid]; }); });