2012-05-03 5 views
5

私はゲームサーバーを作っています。私はSSHからサーバーを実行した後にコマンドを入力したいと思います。 例:addbot、generatemap、kickplayerなどNode.jsでサーバーが実行されているときにコンソールコマンドを実装する方法

ハーフライフまたは他のゲームサーバーのようなものです。 Node.jsを自分のコマンドに耳を傾けても、サーバーをSSHで実行し続けるにはどうしたらいいですか?

答えて

10

あなたはこのようprocess.stdinを使用することができます偉大な作業を

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); 
}) 
+0

、おかげで:

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のようなヘルパーライブラリを使用することができます! – Nick

+0

"quit"コマンドがうまくいかない:P "text"変数のように見える+このテキスト+ "enter";)これを削除するには? – Piotrek

+0

@ Ludwik11を使用する.trim() – Masterakos

関連する問題