0

您好,我一直在尝试使用 node-pty 编写远程编译器。基本逻辑是我的客户端套接字是否从前端应用程序中获取包含语言和源代码的事件。首先我检查语言,然后在当前工作目录中创建一个目录。然后创建一个文件并在其中附加源代码,使用随机名称保存。尝试使用 node-pty 进行编译并将结果发回。

这里是源码,

const { io } = require('socket.io-client');
const terminal = require('node-pty');
const os = require('os');
const fs = require('fs')


const socket = io('http://192.168.1.13:4805');

socket.on(`${os.hostname()}_compile`, (data) => {
    if (data.language === "py") {
        try {
            // Generate random string of length 10
            const randomString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
            // get current working directory to store the file
            const cwd = process.cwd();
            // create directory named 'py' if not exists
            if (!fs.existsSync(`${cwd}/py`)) {
                fs.mkdirSync(`${cwd}/py`);
            }
            fs.writeFileSync(`${cwd}/py/${randomString}.py`, data.code);

            // execute the file
            const pty = terminal.spawn('powershell.exe', [], {
                name: 'xterm-color',
                cols: 80,
                rows: 30,
                cwd: `${cwd}/py`,
                env: process.env
            });

            pty.write(`python ${cwd}/py/${randomString}.py \r`);

            pty.onData('data', (out) => {
                
                console.log('Must be problem with only out');
                //console.log(out);
                /*socket.emit(`compile_result`, {
                    out: out,
                    language: data.language,
                });*/
                //pty.kill();
            });

            pty.onExit((code) => {
                fs.unlinkSync(`${cwd}/py/${randomString}.py`);
            });

            pty.write('ls\r')



        } catch (error) {
            console.log("Throwing : " + error);

            const errorEmit = {
                'source': os.hostname(),
                'error': 'Error in writing file'
            };

            socket.emit('error', errorEmit);
        }
    }
    else if (data.language === "c") {}
    else if (data.language === "cpp") {}
    else if (data.language === "java") {}
});


我正在收到错误

            pty.onData('data', (out) => {

               
                console.log(out);
                /*socket.emit(`compile_result`, {
                    out: out,
                    language: data.language,
                });*/
                //pty.kill();
            });

错误日志如下

D:\Website\Dunix\containers\compiler\compiler-node\node_modules\node-pty\lib\eventEmitter2.js:40
            queue[i].call(undefined, data);
                     ^

TypeError: queue[i].call is not a function
    at EventEmitter2.fire (D:\Website\Dunix\containers\compiler\compiler-node\node_modules\node-pty\lib\eventEmitter2.js
:40:22)
    at Socket.<anonymous> (D:\Website\Dunix\containers\compiler\compiler-node\node_modules\node-pty\lib\terminal.js:85:6
1)
    at Socket.emit (node:events:532:35)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:285:11)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)
[nodemon] app crashed - waiting for file changes before starting...

有什么我做错了吗?

4

0 回答 0