mmo-server

0.1.1 • Public • Published

mmo-server

Simple NodeJS Massive-Multiplayers-Online game server.

This is an HTTP server, more suited for HTML5 games. By default, it will serve static pages from a specified location (option www). But you can register URLs to be Javascript services.

Here is an example of how to use this module to create your own server:

var Server = require( "mmo-server" );
 
var server = new Server();
 
server.register(
  // Name of the service.
  'ADD',
  // @param {string} context.name - Service name.
  // @param {any} context.data - Input of the service.
  // @param {function} context.resolve - Function to call when the service succeed.
  // @param {function} context.reject - Function to call when the service succeed.
  function ( context ) {
    if ( !Array.isArray( context.data ) ) {
      context.reject( "Argument for `sum` must be an Array!" );
    } else {
      var sum = context.data.reduce( function ( acc, val ) {
        return acc + parseFloat(val);
      }, 0 );
      context.resolve( {
        input: context.data,
        sum: sum,
        average: sum / context.data.length
      } );
    }
  }
);
 
server.start( {
  // Path of static files.
  root: "./www",
  // Port to listen on. If not defined (or defined to zero), the first free port
  // will be automatically selected.
  port: 8000,
  // Function called as soon as the server starts successfully.
  // `args` is an object with the following attributes:
  // * `address`: hostname or IP address.
  // * `port`: port on which the server is listening.
  onStart: function ( args ) {
    console.log( "Server started on http://" + args.address + ":" + args.port );
  },
  // Function called if the server failed to start.
  // `err` is the error message.
  onFailure: function ( err ) {
    console.error( "Server failed to start due to the following error:\n", err );
  }
} );

If you want to test this mini server, just create a mini project like this:

mkdir my-project
cd my-project
npm install --save mmo-server

Then, create the file test.js in your new folder my-project and fill it with the code of our example. Come back in the folder and type:

node test.js

Your server is up and running!

You can test it with this command:

firefox "http://localhost:8000/ADD?[3,7]"

Client

Here is an example of a browser client using the summation service described in the previous example:

window.addEventListener( "DOMContentLoaded", function () {
  svc( "sum", [3,7] ).then( function ( result ) {
    alert( "3+7=" + result );
  } );
} );
 
// You can use this function as is to send queries to a specific service.
// The return is a Promise.
function svc( name, data ) {
  return new Promise( function ( resolve, reject ) {
    fetch( name, {
      method: "POST",
      body: JSON.stringify( data )
    } ).then(
      function ( response ) {
        if ( response.ok ) {
          response.text().then( function ( text ) {
            try {
              resolve( JSON.parse( text ) );
            } catch ( ex ) {
              reject( "Invalid JSON: " + text );
            }
          } );
        } else {
          response.text().then( function ( text ) {
            reject( "Error " + response.status + "" + text );
          } )
        }
      }
    ).catch( reject );
  } );
}

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i mmo-server

      Weekly Downloads

      0

      Version

      0.1.1

      License

      GPL-3.0

      Last publish

      Collaborators

      • tolokoban