title: Node.js client and server game using Telnet
body: <p>I'm trying to create a basic game (only text) using Node.js, and it's 'net' library.<br>
  I'm hitting a bit of a wall. I can't seem to figure out how to prompt the user to enter some information, and wait for the user to enter said information.<br><br></p>
  
  <p>Here's the code I have so far. It's fairly basic, and will print out 2 lines for you when you run the client, and then hang. It's at that point I want the user to be able to enter information. I'm unsure of a few things here:<br>
  1. How do I enable the user to type? (more specifically, is it client or server side)<br>
  2. How do I send that data to the server when enter is pressed?<br>
  <br>
  I have read up on some documentation as far as Telnet goes, and it leads me to my last question: is the Telnet module in Node really the right one for this, or is there a better alternative for client/server creation and communication?</p>
  
  <p><br><br>
  Client Code:</p>
  
  <pre><code>var connect = require('net');
  
  var client = connect.connect('80', 'localhost');
  console.log('Connection Success!\n\n');
  client.on('data', function(data) {
    // Log the response from the HTTP server.
    console.log('' + data);
  }).on('connect', function() {
    // Manually write an HTTP request.
    //I'm assuming I could send data at this point here, on connect?
  }).on('end', function() {
    console.log('Disconnected');
  });
  </code></pre>
  
  <p><br><br>
  Server Code:</p>
  
  <pre><code>var net = require('net');
  
  var sockets = [];
  
  function cleanInput(data) {
      return data.toString().replace(/(\r\n|\n|\r)/gm,"");
  }
  
  function receiveData(socket, data) {
      var cleanData = cleanInput(data);
      if(cleanData === "quit") {
          socket.end('Goodbye!\n');
      }
      else {
          for(var i = 0; i&lt;sockets.length; i++) {
              if (sockets[i] !== socket) {
                  sockets[i].write(data);
              }
          }
      }
  }
  
  function closeSocket(socket) {
      var i = sockets.indexOf(socket);
      if (i != -1) {
          sockets.splice(i, 1);
      }
  }
  
  function newSocket(socket) {
      sockets.push(socket);
      socket.write('Welcome to the Battleship Server!\n\n');
      socket.write('Please enter a username: ');
      socket.on('data', function(data) {
          receiveData(socket, data);
      })
      socket.on('end', function() {
          closeSocket(socket);
      })
  }
  
  
  var server = net.createServer(newSocket);
  server.listen(80);
  </code></pre>
  
  <p><br>Thanks in advance!</p>