Skip to content

Commit

Permalink
Adds logs again in server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
silamon committed Oct 9, 2022
1 parent 0ba1230 commit fb91b01
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions demo/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();
});
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -143,6 +153,7 @@ function startServer() {
console.log('Closed terminal ' + term.pid);
// Clean things up
delete terminals[term.pid];
delete logs[term.pid];
});
});

Expand Down

0 comments on commit fb91b01

Please sign in to comment.