我正在尝试创建一个web-based terminal emulator
using node.js
using node-pty
。我能让事情顺利进行。
我的代码如下
const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const os = require("os");
const pty = require("node-pty");
const shell = os.platform() === "win32" ? "powershell.exe" : "bash";
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
io.on("connection", (socket) => {
let ptyProcess = pty.spawn(shell, [], {
cwd: "path/to/directory",
env: process.env,
});
socket.on("start", (data) => {
ptyProcess.on("data", function (output) {
socket.emit("output", output);
console.log(ptyProcess);
});
ptyProcess.write("./result.out\n");
});
socket.on("input", (data) => {
ptyProcess.write(data);
ptyProcess.write('\n');
});
});
server.listen(3000, () => {
console.log("listening on *:3000");
});
我在这里尝试了以下给出的 c 程序。
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int choice, num, i;
unsigned long int fact;
while(1)
{
printf("1. Factorial \n");
printf("2. Prime\n");
printf("3. Odd\\Even\n");
printf("4. Exit\n\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter number:\n");
scanf("%d", &num);
fact = 1;
for(i = 1; i <= num; i++)
{
fact = fact*i;
}
printf("\n\nFactorial value of %d is = %lu\n\n\n",num,fact);
break;
case 2:
printf("Enter number:\n");
scanf("%d", &num);
if(num == 1)
printf("\n1 is neither prime nor composite\n\n");
for(i = 2; i < num; i++)
{
if(num%i == 0)
{
printf("\n%d is not a prime number\n\n", num);
break;
}
}
/*
Not divisible by any number other
than 1 and itself
*/
if(i == num)
{
printf("\n\n%d is a Prime number\n\n", num);
break;
}
case 3:
printf("Enter number:\n");
scanf("%d", &num);
if(num%2 == 0) // 0 is considered to be an even number
printf("\n\n%d is an Even number\n\n",num);
else
printf("\n\n%d is an Odd number\n\n",num);
break;
case 4:
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
exit(0); // terminates the complete program execution
}
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
所以这基本上是一个菜单驱动的程序。一旦我点击输入 4,它应该退出程序。程序退出正在发生。
我的问题是,在我输入 4 之后,程序会给出一个带有一些数据的输出事件,bash3.2$
它看起来像一个正常的输出
有什么方法可以识别最后一个输出并退出 pty 进程?
请帮忙。提前致谢。