Waiting for player input using Node JS

Related searches

I have created a multiplayer poker game for Facebook using HTML5 (client) and Node (server).

I am in the process of writing the server code to allow the player to call, raise, fold, etc. The server sends a message to the HTML5 client requesting the particular player call, raise, etc, but the nature of Javascript is that its non-blocking. I need to make Node wait for the player repsonse before moving on to the next player at the table.

I have had a look at setTimeout, promises, anonymous function and I'm completely lost.

I think that the best approach is to use web socket.

With WS you can open a connection between client and server.

This connection remains opened until something close it.You can send and receive messages on the socket

Server can send messages to clients and clients can send messages to server

Waiting for user to enter input in Node.js, Here's another way that has no dependencies (readline is built-in) const readline = require('readline'); function askQuestion(query) { const rl� Capturing user input in a Node.js application. In a previous article, we saw how Node.js could accept parameters sent via the CLI. In that article, we mentioned that Node.js is also capable of "waiting for user input". This article discusses how to create an application in Node.js that will await input from a user and act on it.

I managed to work out a solution to my own problem. I needed to wait for the user to bet before the Javascript code continues. I achieved this by using a Javascript promise and timers.

  let bet_marker = false;

  // socket.io response from client
  socket.on('bet', function() {
      bet_marker = true;
  });


  const aPromise = new Promise((accept, reject) => {

    setTimeout(function() {
        clearInterval(sI1);
        reject();
    }, 1000);

    let sI1 = setInterval(function() {
        if(bet_marker == true)
        {
            clearInterval(sI1);
            accept();
        }
    }, 500);
});
try {
    console.log("User Bet");
    await aPromise;
} 
catch(e)
{   
    console.log("Time Ran Out);
    console.log(")
}

In my code I create a promise, a setInterval and a setTimeout. The await aPromise statement waits for the promise to call the accept() statment before proceeding. The setInterval function looks every 500ms to see if bet_marker variable has changed. This only changes when a bet is recieved from the client. Once changed we know a bet has been received and the accept() function is called completing the promise. I also have a setTimeout statement in the code that will call that will reject the promise after 10000ms (10s). This gives the player 10 seconds to respond with their bet, but will automatically fold the user if no bet has been received in that time.

I hope that makes sense as I am not a professional programmer...

Javascript wait for user input, In Node. var counter = 0 intervalId = 0; function myLoopFunction() { intervalId Wait() which provided a way how to wait for user input within dialog without necessarily prompting user for input. The player must hit a button to select an action. Now, I have changed the logic a bit, I want to take the input from users and then, store the value in an array. My problem is that I am not able to figure out, how to do this using node.js. I have installed the prompt module using npm install prompt, and also, have gone through the documentation, but nothing is working.

I would just call the next player's "run" function after the current player has made their move.

No need for Promises or anonymous functions.

Node js, of subsequent lines of code while waiting for user input in node js - javascript. ({ input: process.stdin, output: process.stdout }); rl.question("Enter player name ? cd < Your node js current project directory > npm install prompt. After install you can see a node_module folder is created in current project directory. Be sure you run above command in current project folder. Use node js Prompt module to get user input from command line example code.

Cooperative asynchronous JavaScript: Timeouts and intervals , This tutorial looks at the traditional methods JavaScript has available for running In the following example, the browser will wait two seconds before once this happens, the first player to press their control button will win the game. ↻ no more keyboard input is possible to mess up the final game result. Node.js or Node has a small core group of modules, commonly referred to as the Node Core that is exposed as the public Node API by using we write our applications or we can say that the Node Core implements the public Node API. Some examples of the modules present in the node core are: To work with file systems we have a fs module.

Package, JZZ.js allows sending, receiving and playing MIDI messages in Node.js and all This will close MIDI inputs, // otherwise Node.js will wait for MIDI input forever. It’s also possible to use setTimeout (or its cousin setInterval) to keep JavaScript waiting until a condition is met. For example, here’s how you might use setTimeout to wait for a certain

Node.js, being an asynchronous platform, doesn't wait around for things like file I/O to finish - Node.js uses callbacks. A callback is a function called at the completion of a given task; this prevents any blocking, and allows other code to be run in the meantime.

Comments
  • You're probably going to want a Barrier of some kind