npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mmo-server

v0.1.1

Published

Simple NodeJS Massive-Multiplayers-Online game server

Downloads

9

Readme

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 );
  } );
}