在 Qt 应用程序和 nodejs 服务器之间交换数据

Exchange data between Qt Application and nodejs server

我在 nodejs 中创建了一个 Web 服务器架构,它在 运行 时提供网页。 另一方面,我有一个 Qt GUI 应用程序。 我需要在这两者之间建立一个接口。 我已经使用 QProcess 创建了这两者之间的连接(服务器现在从此 Qt 应用程序启动) 现在我想在它们之间交换一些数据,例如 GUI 和返回网页上的随机消息。 stdin/out在这里有用吗? 你能指导我完成这个吗?这是我的服务器代码:

 `function init() {

var __website = path.join(__dirname, 'Website/');
var __css = path.join(__dirname, 'css/');

app.use(bodyParser.json());
app.use(express.static(path.join(__website)));
app.get('/', function (req, res, next) {
    res.render('index', function (err, html) {
        res.send(html);
    });
});
app.get('/data/:iValue', function( req, res) {
process.stdin.on('readable', function() {
  var chunk = process.stdin.read();
  if (chunk != null) {
    res.json({response: chunk.toString("UTF-8")});
     }
 });
   process.stdout.write('request_data:' + temperature);

});
}
function initSocket() {
var Session = require('express-session');
var MemoryStore = require('session-memory-store')(Session);
var session = new Session({
 store: new MemoryStore(),
 secret: 'supersecret',
 resave: true,
 saveUninitialized: true
})

//io = require('socket.io')(https);
io = new ioServer();
io.use(ios(session));
// In-memory storage
// Begin listening for a websockets connection
io.on('connection', function (socket) {
    socket.emit('setRange', data.min, data.max);
    socket.emit('setEmails', data.emails);
    setInterval(function () {
    var temperature;
        //var temperature = Math.floor((Math.random() * 20) + 15);
        socket.emit('updateTemperature', {
            temperature: temperature
        });
        if (temperature < data.min || temperature > data.max) {
            // console.log('alert! ' + temperature + ' min:' + data.min + ' 
max:' + data.max)
            sendEmail(temperature);
            socket.emit('alert', {
                temperature: temperature,
                message: 'Temp is outside of Value Range'
            });
        }
    }, 2000);`

我认为您不必使服务器成为 GUI 程序的子进程,反之亦然。

您需要一些 IPC(Inter-process communication)mechanism here, such as shared memory or named pipe.

而通常用于网络编程的Socket也可以用来使两个进程可以通信。

(我刚刚发现 node.js 没有 FIFO(named pipe)shared memory 包。所以,似乎只有使用 Socket.)

是的,QProcess可以抓取stdoutstderr。您可以使用 readyReadStandardOutput signal emitted by the process then read by any of qprocess read methods like readAllStandardOutput().. also you can have your gui send messages to nodejs standard input with QIODevice::write(const char *data) 继承函数收听它的输出。