笔记:Nodejs 实时向 C++ 编译的可执行文件给予输入和获取输出

背景

我和几个同学当时想要去做一个多人联机的平面小游戏,但需要渲染视野。然而我们不会,只好请教大佬,而大佬不会 Javascript,所以他给我们的是 C++ 编译后的可执行文件,这就产生了延时。


原始方案

我当时写的是每一次游戏循环都运行一次程序,开启进程后向 stdin 加入数据,再从 stdout 获取。

就像这样:

js 复制代码
const exec=require('child_process').exec;

function runCommand(command,stdin)
{
    return new Promise((res,rej)=>{
        let child=exec(command,(err,stdout,stderr)=>{
            err||stderr? rej([err,stderr]):res(stdout));
        });
        child.stdin.write(stdin);
        child.stdin.end();
    });
}

运行起来的时候确实是可以用了,但是一多人就会有卡顿。

测试的时候发现,这种写法每一次都需要开启一个进程,再把这个进程关掉,这样就造成了时间上非常大的浪费,直接占掉至少 20ms 。这对于 30帧 的游戏来说都是不可理喻的。


改进写法

后来,我就想能不能在服务器一开始的时候就开启一个进程,而后就一直开着,服务器关了它才关。于是我先联系大佬把他的程序加上 while(true),而后查阅了大量资料。

后来我们通过 ChatGPT 发现,在 C++ 程序输出的时候 手动把缓冲区清空 ,就可以在 stdout 中得到输出。于是我们写出了下面的代码:

js 复制代码
const spawn=require('child_process').spawn;

const childProcess=spawn('./modules/vision.exe',[],{stdio:['pipe','pipe','inherit']});
childProcess.stdout.setEncoding('utf8');

var nowOutput=``;

childProcess.stdout.on('data',(data)=>{
    nowOutput=data.toString();
});

process.stdin.on('end',()=>{
    childProcess.kill();
});

function runCommand(stdin)
{
    return new Promise((res,rej)=>{
        childProcess.stdin.write(stdin);
        res(nowOutput);
    });
}

最后延时问题得到解决。

相关推荐
一方热衷.1 小时前
YOLO26-Seg ONNXruntime C++/python推理
开发语言·c++·python
Never_Satisfied3 小时前
在JavaScript / Node.js中,package.json文件中的依赖项自动选择最新版安装
javascript·node.js·json
仰泳的熊猫3 小时前
题目2194:蓝桥杯2018年第九届真题-递增三元组
数据结构·c++·算法
2301_803554523 小时前
linux 以及 c++编程里对于进程,线程的操作
linux·运维·c++
日更嵌入式的打工仔4 小时前
个人笔记3
笔记
小糯米6015 小时前
C++ 排序
c++·算法·排序算法
yhole5 小时前
如何升级node.js版本
node.js
Luna-player5 小时前
vue3,单页应用学习笔记
node.js
EverestVIP5 小时前
c++前置声明的方式与说明
开发语言·c++