1
votes

I would like to create a multiplayer HTML5 semi-real-time game using node.js and socket.io. The frame rate is 3 fps. Players use the arrow keys to move. All game objects move in straight lines (players move horizontally or vertically). Players press Page Up/Down to speed up/slow down. This is my first animated HTML5 game, and my first heavy duty JavaScript project.

I went through a tutorial called "Creating a real-time multiplayer game with WebSockets and Node.js" (click here). This tutorial displays a black square for each player with arrow key movement. Unfortunately it only works on one computer (but multiple browser tabs). You have to point your browser(s) to the public/index.html file. I would like to modify it so I can join in the game from the other computer on my LAN, by pointing my browser to 192.168.1.4:8000. Eventually I would like my brother to join in by visiting myquadrawebsite.com. I know how to do port forwarding for apache but not node.js. Here are 3 snippets of abridged, high-level code from the tutorial:

// public/index.html
        <script src="http://localhost:8000/socket.io/socket.io.js"></script>
        <script src="js/game.js"></script>
        <script> // Initialise the game
            init();
            animate();
        </script>
// game.js
    var io = require("socket.io");
    var socket, players;
    function init() {
        players = [];
        socket = io.listen(8000);
        setEventHandlers();
    };
    var setEventHandlers = function() {
        socket.sockets.on("connection", onSocketConnection);
    };
    function onSocketConnection(client) {
        util.log("New player has connected: "+client.id);
        client.on("new player", onNewPlayer);
    };
// public/js/game.js
    var remotePlayers, localPlayer, socket;
    function init() {
        localPlayer = new Player(startX, startY);
        socket = io.connect("http://localhost", {port: 8000, transports: ["websocket"]});
        remotePlayers = [];
        setEventHandlers();
    };
    var setEventHandlers = function() {
        socket.on("connect", onSocketConnected);
        socket.on("new player", onNewPlayer);
    };

I have searched high and low for other tutorials about node.js and socket.io, but none of them have helped me so far. (My long term goal is to create an HTML5 game development framework.) If anyone can possibly point me in the right direction, I'd appreciate it. Thanks.

2
Might I suggest looking into Unity Game engine? Reinventing what is already created can often be an easy mistake.Charlie Brown

2 Answers

0
votes

Unfortunately it only works on one computer (but multiple browser tabs)

This strongly suggests that you are running the server on one computer and it is not accessible from the other computer clients (the web browsers).

You should ensure that the code you use in the client uses a URL that is accessible by anybody (any client) trying to access the game e.g.

socket = io.connect("http://localhost", {port: 8000, transports: ["websocket"]});

Definitely won't work for anybody other than the person on the computer running the server.

If you update the URL to 192.168.1.4:8000 and that address is accessible to others then it is much more likely to work.

0
votes

you shouldn't have to point your browser to the public, the address (depending on where you routed it (if your using express it'll just be /index.html)) http://127.0.0.1:8000 (equivalent to localhost) http://127.0.0.1:8000/index.html

I'm about halfway through my first socket.io game and i'd definitely suggest using a service like nodejitsu for deploying or even testing your game