Need help?

I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.

Please feel free to use the form below to contact me.




Making a game server with Node.js (linux or mac)

Have you been thinking of making a game, but are stuck on the server part? This is understandable, as servers can be quite daunting. Back in the old days, you'd have to worry about writing C code with sockets - scary stuff if you're not familiar with it.

Luckily, we now have something called Node.js - it allows us to create a simple server in a matter of minutes. In this article, I'll be showing you the very basics of creating a socket server.

The first thing we'll need to do is install Node.js. You will require the following dependencies:

Mac:

  • Xcode (download on app store for free)
  • git (http://git-scm.com/download/mac)

Linux (Ubuntu):

  • g++
  • curo
  • libssl-dev
  • apache2-utils
  • git-core

For Ubuntu, you can install all those by typing the following:


sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core

Now that we have all that done, we can begin with Node. Navigate to the location that you want the node/ directory to be created, and type the following.


git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

Lets create a basic socket server - here's the code:


var net = require('net');

var server = net.createServer(function (socket) {
  socket.write('Hey there!rn');
  socket.pipe(socket);
});

server.listen(1007, '127.0.0.1');

To start that server, just type the following:


node socket_server.js

You can test it out by using telnet to connect to the server - type this:


telnet 127.0.0.1 1007

That should display "Hey there!".

And that's it - pretty easy stuff. Obviously this is extremely basic, and you'd need to add a lot more code to make that a functional server; but this gets you started.


jon | August 14, 2012 | Comments (0)

Comments


Name (required)
Email (will not be published) (required)
Website

captcha