Game Development Reference
In-Depth Information
Appendix: Setting up node.js
Let's walk through getting node.js up and running. We'll do it first on Windows and then make some
comments about doing it on a UNIX-based system.
Windows
Download the latest Windows version of node.js from www.nodejs.org . As of this writing, the version is
0.6.2. The installer unpacks node.exe (the thing that will run our JavaScript code) and appends the
appropriate directory (for me, C:\Program Files\nodejs ) to the environment path variable. You should find
that directory (if you are having trouble, search your hard drive for node.exe) and make a note of it.
On Windows, there is no Node Package Manager (npm) utility to automate installation of node.js libraries,
so we'll have to install the websocket library manually. To do that, visit the project's web page,
https://github.com/Worlize/WebSocket-Node , and find the option to download the repository as a zip file.
Extract that zip file to the same directory as node.exe. Alongside node.exe, there should now be a
directory with a name like Worlize-WebSocket-Node-0d04b73 (the string you find at the end may be
different). Rename that directory to “websocket”. That's it! The library is installed.
Now, you should place all the source files associated with the bumper car game in a directory of your
choosing. I put mine in c:\bumper , so we'll use that for what follows. We need to update server.js so that it
knows where to find our libraries. Open up server.js and find the lines beginning “ var WebSocketServer =
” and “ var Game = … ”. Replace those with:
var WebSocketServer = require("c:/program files/nodejs/websocket").server;
var Game = require("c:/bumper/game");
where the directories correspond to those on your system. Notice that the slashes are backwards from
normal Windows paths.
Next, in client-multiplayer.js, find the two instances of “ ws://SERVERIP:9001 ”. If your port 9001 is open to
external traffic, then you can replace SERVERIP by your IP address. If not, and you just want to try it out locally,
then replace SERVERIP by localhost (or, equivalently, 127.0.0.1 ). Go to the command line and execute
node c:\bumper\server.js
If you get a complaint that it can't find node, then navigate to the directory containing node.exe and try
executing it again. Finally, update bumper.htm to make sure that it refers to client-multiplayer.js rather
than client-local.js , and then load up bumper.htm in a web browser (or two)!
UNIX
To install node.js on a UNIX-based operating system, you should first check if there is a pre-compiled
package available for you, following the instructions located at https://github.com/joyent/
node/wiki/Installing-Node.js-via-package-manager . If not, you will have to build node.js from its source
code. There are comprehensive instructions for doing that available at https://github.com/joyent/node/
wiki/Installation .
 
Search WWH ::




Custom Search