1

私は Node.js と非同期プログラミング全般に不慣れで、問題があります。再帰ループで継続的な作業サーバーが必要です。この手順は一貫して実装され、各段階は Websotskets を介した非同期受信データで構成される必要があります (これは既に理解されています)、またはタイマーの有効期限が切れると次の段階に進みます。些細な作業ではないことは承知していますが、実装方法、どのライブラリが役立つでしょうか? Step.jsおよび Node.js イベントを理解しようとしましたが、これが必要ですか?

これをすべて同期して書いた場合:

//CODE FOR STACKOVERFLOW
var previous_round = 'qwerty';// this is string, selected in previous cycle
var round_users = users;// 'users' is array of soceket.id all connected users and here a put them into new array 'round_users' with sockets for current cycle
io.sockets.emit('users_round', { number: round_users.length }); // send to users information about number of users in new round
for (var i = 0; i < round_users.length; i++) {
      io.sockets.socket(round_users[i]).emit('round', { text: previous_round });//for each socket in 'round_users' emit event to enter chat message
}                                                                               // and send selected in previous round message
var messages = []; // array of users messages
//now we must listen users events and start timer for next stage
  //listen users events
  for (var i = 0; i < round_users.length; i++) {
        io.sockets.socket(round_users[i]).on('message', function (data) {
          messages[messages.length] = data.text;//write text from users to array
          if (messages.length == round_users.length) { /* GO TO NEXT STAGE */ } // it's after previous operation in this function
        });
  } 
  //or set timeout
  setTimeout(/* GO TO NEXT STAGE */,15000);
for (var i = 0; i < round_users.length; i++) {
      io.sockets.socket(round_users[i]).emit('voting', { messages_array: messages });//for each socket in 'round_users' emit event to vote perfect chat message
}                                                                                    // and send messages, which they send to server
//i'm not quite sure about this decision of vote counting :-)
var votes = []; //array with users votes
for (var i = 0; i < messages.length; i++) {
      votes[i] = 0;
}   
//now we must listen users events and start timer
  //listen users events
  for (var i = 0; i < round_users.length; i++) {
        io.sockets.socket(round_users[i]).on('vote', function (data) {
          votes[data.number]++;//increment selected message
          if (votes.length == messages.length) { /* GO TO NEXT STAGE */ } // it's after previous operation in this function
        });
  } 
  //or set timeout
  setTimeout(/* GO TO NEXT STAGE */,10000);
var max_id = 0; //now select max number from 'votes' array
for (var i = 0; i < votes.length; i++) {
      if (votes[i]>votes[max_id]) {max_id = i;} //yet without the same values
}   
var previous_round = messages[max_id]; //set up string value of selected message
//restart cycle

シンタックスハイライト付きのペーストビンのこのコード

4

1 に答える 1

2

最も基本的な意味での再帰は、自分自身を何度も呼び出す関数に過ぎませんが、このノードのインスタンスでは、この再帰中に他のイベントを発生させるためにprocess.nextTick()を利用することができます。

これは、あなたがそれについて行くことができるかもしれない一つの方法の簡単な例です:

function someFunction(args, callback) {
  // Check requirements
  if (typeof callback !== 'function') return console.log('Callback required');
  if (typeof args === 'undefined') return callback('Arguments required');

  // Set defaults
  var err = false;
  var result = false;

  // Do other stuff
  anotherFunctionWithAcallback(args, function(err, result) {
    if (err) return callback(err);
    // This way, the nextTick only occurs after processing completes
    return callback(err, result); 
  }
}

(function loop(stage) {
  // When the stage argument is not provided, default to stage 1
  stage = (typeof stage === 'undefined') ? 1 : stage;
  switch(stage) {
    case 1:
      /* stage 1 */
      someFunction(args, function(err, result){
        process.nextTick(loop(2));
      });
    break;
    case 2:
      /* stage 2 */
      someFunction(args, function(err, result){
        process.nextTick(loop(3));
      });
    break;
    case 3:
      /* stage 3 */
      someFunction(args, function(err, result){
        // No stage argument, restart at stage 1 by default
        process.nextTick(loop());
      });
    break;
  }
})(); // Execute this function immediately
于 2012-05-03T01:25:08.270 に答える