Use ws directly to handle new exec ws route

This commit is contained in:
Girish Ramakrishnan
2017-08-18 17:56:01 -07:00
parent e3040b334d
commit 537fbff4aa
5 changed files with 91 additions and 84 deletions

View File

@@ -461,7 +461,7 @@ function exec(req, res, next) {
});
}
function execWebSocket(ws, req, next) {
function execWebSocket(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
debug('Execing websocket into app id:%s and cmd:%s', req.params.id, req.query.cmd);
@@ -487,24 +487,28 @@ function execWebSocket(ws, req, next) {
console.log('Connected to terminal');
duplexStream.on('end', function () { ws.close(); });
duplexStream.on('close', function () { ws.close(); });
duplexStream.on('error', function (error) {
console.error('duplexStream error:', error);
});
duplexStream.on('data', function (data) {
if (ws.readyState !== WebSocket.OPEN) return;
ws.send(data.toString());
});
req.clearTimeout();
ws.on('error', function (error) {
console.error('websocket error:', error);
});
ws.on('message', function (msg) {
duplexStream.write(msg);
});
ws.on('close', function () {
// Clean things up, if any?
res.handleUpgrade(function (ws) {
duplexStream.on('end', function () { ws.close(); });
duplexStream.on('close', function () { ws.close(); });
duplexStream.on('error', function (error) {
console.error('duplexStream error:', error);
});
duplexStream.on('data', function (data) {
if (ws.readyState !== WebSocket.OPEN) return;
ws.send(data.toString());
});
ws.on('error', function (error) {
console.error('websocket error:', error);
});
ws.on('message', function (msg) {
duplexStream.write(msg);
});
ws.on('close', function () {
// Clean things up, if any?
});
});
});
}