Code Monkey home page Code Monkey logo

flatpack's Introduction

flatpack

Wrapper for msgpack-lite that flattens objects

Usage

shared/socket_helper.js

In a shared file, define new Flatpack models for each event. Leave the value of each property empty unless it is an object.

import { Flatpack } from './flatpack.js';

const fp = new Flatpack();

fp.add('laserFire', {
  userID: '',
  team: '',
  position: {
    x: '',
    y: '',
    angle: ''
  }
});

fp.add('laserHit', {
  origin: {
    userID: '',
    team: '',
    pointsAwarded: ''
  },
  target: {
    userID: '',
    team: ''
  }
});

export { fp };

client/client_socket.js

On the sending side (the client in this example), use a Flatpack model to encode data.

import { fp } from '../shared/socket_helper.js';

sendLaserFire(data) {
  const buffer = fp.encode('laserFire', data);
  this.socket.emit('laserFire', buffer);
}

sendLaserHit(data) {
  const buffer = fp.encode('laserHit', data);
  this.socket.emit('laserHit', buffer);
}

server/server_socket.js

On the receiving end (the server in this example), use a Flatpack model to decode data.

import { fp } from '../shared/socket_helper.js';

recvLaserFire(buffer) {
  const data = fp.decode('laserFire', buffer);
  this.game.handleLaserFire(data);
}

recvLaserHit(buffer) {
  const data = fp.decode('laserHit', buffer);
  this.game.handleLaserHit(data);
}

Additional Features

Flatpack can also list all the available events as unique integers, so rather than sharing an named event string through the socket, we can refrence an event with an integer.

import { fp } from '../shared/socket_helper.js';

const EVENTS = fp.list();

sendLaserFire(data) {
  const buffer = fp.encode(EVENTS.laserFire, data);
  this.socket.emit(EVENTS.laserFire, buffer);
}

socket.on(EVENTS.laserFire, buffer => this.recvLaserFire(buffer));

recvLaserFire(buffer) {
  const data = fp.decode(EVENTS.laserFire, buffer);
  this.game.handleLaserFire(data);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.