phretaddin / schemapack

Create a schema object to encode/decode your JSON in to a compact byte buffer with no overhead.
MIT License
452 stars 33 forks source link

Shared schemas between client and server #34

Open Abul22 opened 6 years ago

Abul22 commented 6 years ago

I'm trying to have a single file called shared.js to include on the server and client rather than dupe for both. But Can't seem to do this as I just get 'schemapack is undefined' when running with node...Even though it's declared and defined above it. Help would be appreciated... Thank you!

Eg.

shared.js


const textMsg = schemapack.build({ __message: 'uint8', msg: 'string' });

try { module.exports = {textMsg}; } catch (err) { //We are not running as node.js module }


Server:


const schemapack = require('schemapack'); require('./public/shared');


Client:



endel commented 5 years ago

I'm working on a similar binary serializer, which allows you to perform some "reflection": https://github.com/colyseus/schema

You define the type, and then you can encode/decode the type itself.

import { Schema, type, Reflection } from "@colyseus/schema";

class MyState extends Schema {
  @type("string")
  name: string;
}

var serverState = new MyState();

// byte array representing all related types to MyState, 
// you can send this over from the server to the client
var reflected = Reflection.encode(serverState);

// instantiate `MyState` in the client based on the reflected bytes
var clientState = Reflection.decode(reflected);
anooshcnayak commented 3 years ago

@endel Is https://github.com/colyseus/schema faster than schemapack - are there any benchmarks?