I give up! I need help.
Been trying for the last week to think of ways to hook-up Xterm.js with QEWD. I have just had success with creating a new express application on a separate port, and then connecting to it. When I try to use the embedded express in QEWD, the websocket connection fails. It seems that you can't have two websocket connections open at the same time on the same port.
Ideally, I don't even want to do the direct websocket thing: I would like QEWD to magically hook-up a websocket for me to a subprocess.
For reference, here is my code in qewd.js (I am just trying to get it to work...)
// vista terminal stuff
var xp = qewd.intercept();
var terminals = {},
logs = {};
/*
*/
var expressWs = require('express-ws')(
xp.app);
var os = require('os');
var pty = require('node-pty');
xp.app.post('/ewd-vista/vista-terminal', function(req, res) {
term = pty.spawn('mumps', ['-run', 'ZU'], {
name: 'xterm-color',
cols: 80,
rows: 24,
cwd: process.env.PWD,
env: process.env
});
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();
});
xp.app.post('/ewd-vista/vista-terminal/:pid/size', function (req, res) {
var pid = parseInt(req.params.pid),
cols = parseInt(req.query.cols),
rows = parseInt(req.query.rows),
term = terminals[pid];
term.resize(cols, rows);
console.log('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.');
res.end();
});
xp.app.ws('/ewd-vista/vista-terminal/:pid', function (ws, req) {
var term = terminals[parseInt(req.params.pid)];
console.log('Connected to terminal ' + term.pid);
ws.send(logs[term.pid]);
term.on('data', function(data) {
try {
ws.send(data);
} catch (ex) {
// The WebSocket is not open, ignore
}
});
ws.on('message', function(msg) {
term.write(msg);
});
ws.on('close', function () {
term.kill();
console.log('Closed terminal ' + term.pid);
// Clean things up
delete terminals[term.pid];
delete logs[term.pid];
});
});
/*
var express = require('express');
var myApp = express();
var expressWs = require('express-ws')(myApp);
myApp.use(function (req,res,next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '
http://localhost:' + config.port.toString());
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
myApp.post('/ewd-vista/vista-terminal', function(req, res) {
term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : 'mumps', ['-run', 'ZU'], {
name: 'xterm-color',
cols: 80,
rows: 24,
cwd: process.env.PWD,
env: process.env
});
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();
});
myApp.post('/ewd-vista/vista-terminal/:pid/size', function (req, res) {
var pid = parseInt(req.params.pid),
cols = parseInt(req.query.cols),
rows = parseInt(req.query.rows),
term = terminals[pid];
term.resize(cols, rows);
console.log('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.');
res.end();
});
myApp.ws('/ewd-vista/vista-terminal/:pid', function (ws, req) {
var term = terminals[parseInt(req.params.pid)];
console.log('Connected to terminal ' + term.pid);
ws.send(logs[term.pid]);
term.on('data', function(data) {
try {
ws.send(data);
} catch (ex) {
// The WebSocket is not open, ignore
}
});
ws.on('message', function(msg) {
term.write(msg);
});
ws.on('close', function () {
term.kill();
console.log('Closed terminal ' + term.pid);
// Clean things up
delete terminals[term.pid];
delete logs[term.pid];
});
});
myApp.listen(8081);
*/