Shared schemas between client and server
Opened this issue · 2 comments
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:
<script src='./schemapack.min.js'></script> <script src='./shared.js'> </script>
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);
@endel Is https://github.com/colyseus/schema faster than schemapack - are there any benchmarks?