Wednesday, December 7, 2011

Install nowjs in Windows

First you have to go through the process of installing node.js in windows. I have detailed the process there.

There's two requirements for nowjs to run on windows:

Microsoft Visual C++ Runtime:


Go ahead and install it if you don't already have it.

Install Microsoft Visual C++ Runtime (4.8 MB)

socket.io:

So we can open a CMD window and type:

C:\progra~1\nodejs\npm install socket.io

If your node.js install is in a different directory then change the path accordingly.

NPM will install it to:

C:\Program Files\nodejs\node_modules\socket.io



Good.  Now we need to obtain the nowjs windows build here:

https://github.com/Flotype/now/zipball/windows

The file will read something like Flotype-now-0.7.6-3-gb41b2b2.zip

Unzip it to a folder.  Inside that folder you'll have another folder with a similar name.

So:  ~\Downloads\Flotype-now-0.7.6-3-gb41b2b2\Flotype-now-b41b2b2

Let's rename the second folder to now

So: ~Downloads\Flotype-now-0.7.6-3-gb41b2b2\now

Copy out the now folder and paste it to:

C:\Program Files\nodejs\node_modules\

So: C:\Program Files\nodejs\node_modules\now

Inside of that now folder there should be subfolders like bin, doc, examples and other items.


You now have nowjs installed within windows as a module for node.js.  It looks like quite a bit of steps, but  in reality it is quite painless and if you followed the steps you've seen that.

Time to test it!

The nowjs people have supplied us with three example projects:

They are located in: C:\Program Files\nodejs\node_modules\now\examples
express_example
helloworld_example
multiroomchat_example

Let's jump to the fun stuff and try and get multiroomchat_example going.

Firstly close down any node.js server CMD window instances you have running.

Open up a new CMD window and type:

C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\now\examples\multiroomchat_example\multiroomchat_server.js

Hit enter.

You now have node.js running the multiroom chat example server.

Take a look by pointing a few browsers to http://localhost:8080/ it will prompt you for a name.

You'll end up with:



And if everything went perfectly you have now installed and tested nowjs.

A multi-room chat example in under 10 minutes, less than that if you're quick!  From this point we have the ability to communicate in real-time to multiple browsers and a server, the basis for a MMORPG.



Install Node.js in Windows

If you're like me you are forced to use windows at work, and I know that is quite a few people.  I'm not going to get into why that's a bad thing or any of the pro-linux talk, it is what it is.  I'm also not going to get into the fact that if you're a gamer chances are you're running windows at home (although I hope http://www.humblebundle.com/ changes all that) as linux isn't the preferred os for gaming, not that it isn't better or worse, again: it is what it is.  For the record, I am pro-linux.

For most of what this blog is about is based on node.js.  So we need a way to install and use node.js in windows.

Let's proceed to http://nodejs.org/#download


That's the windows msi installer, the one we want.  By the time you read this they might be on v1.0.0 for all I know, but I'm guessing it'll be about the same.  Currently the 0.6.5 is ~3 MB.  Let's run it.


And click "Next".

We're almost done.

Let's open up a text file in notepad and copy/paste this test node.js application:


  require('http').createServer(function(req,res){
        res.writeHead(200,'text/plain');
        res.end('Hello World!');
    }).listen('8080');


Save it as server.js to your root directory C:\ or whatever it may be.


So now we run in a command prompt/powershell:
You can bring that up with Windows Key + R and typing in CMD


Your node.exe is located usually <root>\Program Files\nodejs\node.exe
so probably: C:\Progra~1\nodejs\node.exe

node.exe c:\server.js

The server.js is the one I had you create and save to your root directory earlier.

If it's your first time doing this you'll end up with:


Click "Allow access"

Now open up your browser (here I am using Chrome 17)

And let's see if our server.js is running by following the link or typing it in:

http://localhost:8080/


If your results look something like this you've done it!

Node.js is fully installed, running, and tested.  Awesome.

Tuesday, December 6, 2011

Node.js Options Parser

To put it simply options parsing is a way to pass arguments to functions in external files.  This doesn't sound like a big deal, but it is.  Let me explain.

Lets say you have all of your game functions in one giant js file, functions for calculating attack, def, etc.  A month down the road you realize that you want to change just about anything, you'll have to either rewrite a bunch or something far worse. Non-modularity is the hallmark of hard to maintain code or the 10lbs of 'stuff' in a 5lb bag.

Wouldn't it be much easier if we could just keep all of our functions separate?  Pass arguments to them in a generic and modular way?  Update a 20 line file instead of a 1,000+ line file?  We can do all of that with options parsers.

Taken from: http://www.catonmat.net/blog/nodejs-modules-optimist/


This time I'll introduce you to node-optimist - the lightweight options parser library. This library is also written by James Halliday (SubStack), my co-founder of Browserling and Testling.
Wonder how lightweight an options parser can be? Check this out:
var argv = require('optimist').argv;
And you're done! All options have been parsed for you and have been put in argv.
Here are various use cases. First off, it supports long arguments:
#!/usr/bin/env node
var argv = require('optimist').argv;

if (argv.rif - 5 * argv.xup > 7.138) {
    console.log('Buy more riffiwobbles');
}
else {
    console.log('Sell the xupptumblers');
}
Now you can run this script with --rif and --xup arguments like this:
$ ./xup.js --rif=55 --xup=9.52
Buy more riffiwobbles

$ ./xup.js --rif 12 --xup 8.1
Sell the xupptumblers
I know you want to buy more riffiwobbles and sell your xupptumblers.
I'm guessing it's in order of magnitudes more complicated, but looks to be necessary.

Server Side functions

So lets say we have a situation where we have a game where most of the player variables are stored server side, and only showed or used by the player as a "stats" page.  The actual usage of the stats, lets say for calculating the attack power or something to that effect would all take place server side, because it's quicker that way.  So how do we make these 'remote procedure calls' ?

A Node.js module called dnode: dnode-protocol github repo.


Here is what it is. This is the server.js:
var dnode = require('dnode');

var server = dnode({
    mul : function (n, m, cb) { cb(n * m) }
});
server.listen(5050);
And here is the client.js:
var dnode = require('dnode');

dnode.connect(5050, function (remote) {
    remote.mul(10, 20, function (n) {
        console.log('10 * 20 = ' + n);
    });
});
Now when you run client.js, you get the output:
$ node client.js
200

So the potential for this is great.  Computing most things server side will greatly free up client side operations.

Monday, December 5, 2011

Node.js Auto Restart on github change

Title should say it all: When you make a source code change, it will automatically restart the server.  No more manually doing this.  Wonderful for development.

https://github.com/remy/nodemon

From the github project:


nodemon

For use during development of a node.js based application.
nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application.
nodemon does not require any changes to your code or method of development. nodemon simply wraps your node application and keeps an eye on any files that have changed.