@@ -16,7 +16,9 @@ function startServer() {
16
16
var app = express ( ) ;
17
17
expressWs ( app ) ;
18
18
19
- var terminals = { } ;
19
+ var terminals = { } ,
20
+ unsentOutput = { } ,
21
+ temporaryDisposable = { } ;
20
22
21
23
app . use ( '/xterm.css' , express . static ( __dirname + '/../css/xterm.css' ) ) ;
22
24
app . get ( '/logo.png' , ( req , res ) => {
@@ -54,6 +56,10 @@ function startServer() {
54
56
55
57
console . log ( 'Created terminal with PID: ' + term . pid ) ;
56
58
terminals [ term . pid ] = term ;
59
+ unsentOutput [ term . pid ] = '' ;
60
+ temporaryDisposable [ term . pid ] = term . onData ( function ( data ) {
61
+ unsentOutput [ term . pid ] += data ;
62
+ } ) ;
57
63
res . send ( term . pid . toString ( ) ) ;
58
64
res . end ( ) ;
59
65
} ) ;
@@ -72,6 +78,10 @@ function startServer() {
72
78
app . ws ( '/terminals/:pid' , function ( ws , req ) {
73
79
var term = terminals [ parseInt ( req . params . pid ) ] ;
74
80
console . log ( 'Connected to terminal ' + term . pid ) ;
81
+ temporaryDisposable [ term . pid ] . dispose ( ) ;
82
+ delete temporaryDisposable [ term . pid ] ;
83
+ ws . send ( unsentOutput [ term . pid ] ) ;
84
+ delete unsentOutput [ term . pid ] ;
75
85
76
86
// unbuffered delivery after user input
77
87
let userInput = false ;
@@ -131,8 +141,12 @@ function startServer() {
131
141
// WARNING: This is a naive implementation that will not throttle the flow of data. This means
132
142
// it could flood the communication channel and make the terminal unresponsive. Learn more about
133
143
// the problem and how to implement flow control at https://xtermjs.org/docs/guides/flowcontrol/
134
- term . on ( 'data' , function ( data ) {
135
- send ( data ) ;
144
+ term . onData ( function ( data ) {
145
+ try {
146
+ send ( data ) ;
147
+ } catch ( ex ) {
148
+ // The WebSocket is not open, ignore
149
+ }
136
150
} ) ;
137
151
ws . on ( 'message' , function ( msg ) {
138
152
term . write ( msg ) ;
0 commit comments