diff --git a/src/Room.ts b/src/Room.ts index 28ddc96..d248a55 100644 --- a/src/Room.ts +++ b/src/Room.ts @@ -116,19 +116,28 @@ export class Room { return this.onMessageHandlers.on(this.getMessageHandlerKey(type), callback); } - public send(type: string | number, message?: any): void { - const initialBytes: number[] = [Protocol.ROOM_DATA]; + public send(type: T); + public send(type: T, message?: any); + public send(type: string | number | Schema, message?: any): void { + const initialBytes: number[] = type instanceof Schema ? [Protocol.ROOM_DATA_SCHEMA] : [Protocol.ROOM_DATA]; - if (typeof(type) === "string") { - encode.string(initialBytes, type); + if (type instanceof Schema) { + encode.string(initialBytes, type.constructor.name); + } else if (typeof(type) === "string") { + encode.string(initialBytes, type); } else { encode.number(initialBytes, type); } let arr: Uint8Array; - if (message !== undefined) { + if (type instanceof Schema) { + const encoded = new Uint8Array(type.encode(true)) + arr = new Uint8Array(initialBytes.length + encoded.byteLength); + arr.set(new Uint8Array(initialBytes), 0); + arr.set(new Uint8Array(encoded), initialBytes.length); + } else if (message !== undefined) { const encoded = msgpack.encode(message); arr = new Uint8Array(initialBytes.length + encoded.byteLength); arr.set(new Uint8Array(initialBytes), 0);