multyx
Version:
Framework designed to simplify the creation of multiplayer browser games by addressing the complexities of managing server-client communication, shared state, and input handling
53 lines (44 loc) • 1.48 kB
text/typescript
import { CompressUpdate, Update } from "./update";
export default class Message {
name: string;
data: any;
time: number;
native: boolean;
/**
* Constructor for creating messages to send to client
* @param name
* @param data
*/
private constructor(name: string, data: any, native: boolean = false) {
this.name = name;
this.data = data;
this.time = Date.now();
this.native = native;
}
// Send multyx client native instructions
static Native(updates: Update[]) {
const data = [];
for(const update of updates) {
data.push(CompressUpdate(update));
}
return JSON.stringify(data);
}
static Create(name: string, data: any) {
if(name.length == 0) throw new Error('Multyx message cannot have empty name');
if(name[0] == '_') name = '_' + name;
if(typeof data === 'function') {
throw new Error('Multyx data must be JSON storable');
}
return JSON.stringify(new Message(name, data));
}
// Parse message from user
static Parse(str: string) {
try {
const parsed = JSON.parse(str);
if(Array.isArray(parsed)) return new Message('_', parsed[0], true);
return new Message(parsed.name ?? '', parsed.data ?? '', false);
} catch {
return null;
}
}
}