From fb91b01217d6b75102b9bdf4a9fed7e40d7cc633 Mon Sep 17 00:00:00 2001 From: Simon Lamon Date: Sun, 9 Oct 2022 12:16:26 +0000 Subject: [PATCH] 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 0e82f9e9..2a48a21c 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]; }); });