Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
..
Failed to load latest commit information.
CNAME
README.md

README.md

SNEX Documentation

SNEX requires three steps to work.

  1. Create a Session

A session is basically a unique id to which controllers can connect and send events. If you create a game and want users to play it using SNEX, you first set up a session using the SNEX library on your end and then your users needs to know the session id to connect to.

const snex = require('snex');

snex.createSession()
.then(session => {
  console.log(session.id);
});
  1. Create a controller URL

The controller URL is where your users go to see a controller. The SNEX controllers have to know what session to send their signals to thus you have to create a unique URL for your session. There are two ways to create URLs; manually and via the session.

Manual

const URL = `https://snex.io/nes?id=${session.id}`;
console.log(`Go to ${URL} to play!`);

Using session

session.createURL('nes')
.then(link => {
  const URL = link.url;
  console.log(`Go to ${URL} to play!`);
});

When you create a URL using the session, a short code will be stored with SNEX that contain both the session id and the type of controller so that your users can connect using a short URL like snex.io/XAFE.

  1. Handle events

Once a user is connected you will receive a connection event. Think of this as a human plugging in a new controller. The connection event will emit an object representing a connected controller. This object will emit data events when a controller is interacted with. Controllers send events in JSON and will contain the key (button name) of the event and the button's current state. You don't have to decode the JSON yourself, it is handled by the library.

When a user leaves the controller page, a close event will be emitted on the controller object.

session.on('connection', controller => {

  console.log('Controller was connected.');

  controller.on('data', data => {

    // Prints 'Player pressed {key: "START", state: 1}'
    console.log('Player pressed', data);

  });

  contoller.on('close', () => {
    console.log('Controller was disconnected.');
  });
});

Buttons only have two states; 1 for when it is pressed, 0 for when it is released.

Event keys are uppercase strings. For NES they are UP, DOWN, LEFT, RIGHT. SELECT, START, A, B.

Full life cycle example

// Your game
const Game = require('my-space-game');

// SNEX library
const snex = require('snex');

const game = new Game();

snex.createSession()
.then(session => {
  session.on('connection', controller => {
    const player = game.addPlayer();

    controller.on('data', data => {
      if (data.state && data.key === 'A') {
        player.jump();
      }
    }

    controller.on('close', () => {
      game.removePlayer(player);
    });
  });

  session.createURL('nes')
  .then(link => {
    console.log(`Go to ${link.url} to join game!`);
  });
});

Implementation examples

Controllers

We provide three different controllers, NES, Sega Genesis (Sega Mega Drive), SNES (Famicom).

NES

NES

Session link

session.createURL('nes')

Manual URL

console.log(`https://snex.io/nes?id=${session.id}`);

Events

Event JSON
UP pressed {"key":"UP","state":1}
UP released {"key":"UP","state":0}
LEFT pressed {"key":"LEFT","state":1}
LEFT released {"key":"LEFT","state":0}
RIGHT pressed {"key":"RIGHT","state":1}
RIGHT released {"key":"RIGHT","state":0}
DOWN pressed {"key":"DOWN","state":1}
DOWN released {"key":"DOWN","state":0}
SELECT pressed {"key":"SELECT","state":1}
SELECT released {"key":"SELECT","state":0}
START pressed {"key":"START","state":1}
START released {"key":"START","state":0}
B pressed {"key":"B","state":1}
B released {"key":"B","state":0}
A pressed {"key":"A","state":1}
A released {"key":"A","state":0}

SNES / Super Famicom

SNES

SNES

Session link

session.createURL('snes')
session.createURL('snes-us')

Manual URL

console.log(`https://snex.io/snes?id=${session.id}`);
console.log(`https://snex.io/snes-us?id=${session.id}`);

Events

Event JSON
UP pressed {"key":"UP","state":1}
UP released {"key":"UP","state":0}
LEFT pressed {"key":"LEFT","state":1}
LEFT released {"key":"LEFT","state":0}
RIGHT pressed {"key":"RIGHT","state":1}
RIGHT released {"key":"RIGHT","state":0}
DOWN pressed {"key":"DOWN","state":1}
DOWN released {"key":"DOWN","state":0}
SELECT pressed {"key":"SELECT","state":1}
SELECT released {"key":"SELECT","state":0}
START pressed {"key":"START","state":1}
START released {"key":"START","state":0}
X pressed {"key":"X","state":1}
X released {"key":"X","state":0}
Y pressed {"key":"Y","state":1}
Y released {"key":"Y","state":0}
B pressed {"key":"B","state":1}
B released {"key":"B","state":0}
A pressed {"key":"A","state":1}
A released {"key":"A","state":0}

Sega Genesis / Sega Mega Drive

Genesis

Session link

session.createURL('genesis')

Manual URL

console.log(`https://snex.io/genesis?id=${session.id}`);

Events

Event JSON
UP pressed {"key":"UP","state":1}
UP released {"key":"UP","state":0}
LEFT pressed {"key":"LEFT","state":1}
LEFT released {"key":"LEFT","state":0}
RIGHT pressed {"key":"RIGHT","state":1}
RIGHT released {"key":"RIGHT","state":0}
DOWN pressed {"key":"DOWN","state":1}
DOWN released {"key":"DOWN","state":0}
START pressed {"key":"START","state":1}
START released {"key":"START","state":0}
A pressed {"key":"A","state":1}
A released {"key":"A","state":0}
B pressed {"key":"B","state":1}
B released {"key":"B","state":0}
C pressed {"key":"C","state":1}
C released {"key":"C","state":0}