我正在制作一台游戏服务器,我想在从SSH运行服务器后输入命令。例如: addbot,generatemap,kickplayer等。
就像在“半条命”或其他游戏服务器中。如何让Node.js监听我的命令,同时保持服务器在SSH中运行?
发布于 2012-05-03 18:36:56
您可以按如下方式使用process.stdin:
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (text) {
console.log(text);
if (text.trim() === 'quit') {
done();
}
});
function done() {
console.log('Now that process.stdin is paused, there is nothing more to do.');
process.exit();
}否则,您可以使用像prompt https://github.com/flatiron/prompt这样的帮助器库,它允许您执行以下操作:
var prompt = require('prompt');
// Start the prompt
prompt.start();
// Get two properties from the user: username and email
prompt.get(['username', 'email'], function (err, result) {
// Log the results.
console.log('Command-line input received:');
console.log(' username: ' + result.username);
console.log(' email: ' + result.email);
})https://stackoverflow.com/questions/10428684
复制相似问题