簡體   English   中英

如何在Node.js中運行服務器時實現控制台命令

[英]How to implement console commands while server is running in Node.js

我正在制作一個游戲服務器,我想在從SSH運行服務器后輸入命令。 例如:addbot,generatemap,kickplayer等。

就像半條命或任何其他游戲服務器一樣。 如何讓Node.js監聽我的命令並仍然使服務器在SSH中運行?

您可以像這樣使用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();
}

否則,你可以使用幫助庫,如提示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);
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM