From bb8f985bac01d302663aad5c170bd4192bef8f9d Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 7 Dec 2020 20:06:07 -0600 Subject: [PATCH] feat: dehydrated packet sending --- packages/socket/src/client/socket.js | 26 ++++++++++++++++++++------ packages/socket/src/socket.js | 18 +++++++++++++++--- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/packages/socket/src/client/socket.js b/packages/socket/src/client/socket.js index 0dfb9cb..362aee7 100644 --- a/packages/socket/src/client/socket.js +++ b/packages/socket/src/client/socket.js @@ -77,21 +77,35 @@ export default class SocketClient extends decorate(Class) { this.socket.disconnect(); } + static hydrate(latus, [type, data]) { + const Packets = all(latus); + if (!Packets[type]) { + throw new TypeError(`No packet of type '${type}'`); + } + return new Packets[type](data); + } + get id() { return this.socket ? this.socket.id : undefined; } - send(packet, fn) { + static send(latus, socket, packetOrDehydrated, fn) { + const packet = Array.isArray(packetOrDehydrated) + ? this.hydrate(latus, packetOrDehydrated) + : packetOrDehydrated; + debug('sending packet %o', packet); const {id} = packet.constructor; - debug('sending packet %s(%o)', packet.constructor.name, packet.data); - this.socket.binary(true).emit(id, packet.data, fn); + socket.binary(true).emit(id, packet.data, fn); + } + + send(packet, fn) { + this.constructor.send(this.latus, this.socket, packet, fn); } to(channel) { return { - send: (packet) => { - const {id} = packet.constructor; - this.socket.binary(true).to(channel).emit(id, packet.data); + send: (packet, fn) => { + this.constructor.send(this.latus, this.socket.to(channel), packet, fn); }, }; } diff --git a/packages/socket/src/socket.js b/packages/socket/src/socket.js index f357901..28d9c80 100644 --- a/packages/socket/src/socket.js +++ b/packages/socket/src/socket.js @@ -13,6 +13,7 @@ export default class ServerSocket extends decorate(Class) { constructor(socket, latus) { super(); + this.latus = latus; this.socket = socket; const Packets = Object.entries(all(latus)); for (let i = 0; i < Packets.length; i++) { @@ -37,6 +38,14 @@ export default class ServerSocket extends decorate(Class) { this.socket.disconnect(true); } + static hydrate(latus, [type, data]) { + const Packets = all(latus); + if (!Packets[type]) { + throw new TypeError(`No packet of type '${type}'`); + } + return new Packets[type](data); + } + get id() { return this.socket.id; } @@ -57,19 +66,22 @@ export default class ServerSocket extends decorate(Class) { return this.socket.rooms; } - static send(socket, packet, fn) { + static send(latus, socket, packetOrDehydrated, fn) { + const packet = Array.isArray(packetOrDehydrated) + ? this.hydrate(latus, packetOrDehydrated) + : packetOrDehydrated; debug('sending packet %o', packet); const {id} = packet.constructor; socket.binary(true).emit(id, packet.data, fn); } send(packet, fn) { - this.constructor.send(this.socket, packet, fn); + this.constructor.send(this.latus, this.socket, packet, fn); } to(room, packet, fn) { debug('broadcasting to %s', room); - this.constructor.send(this.socket.to(room), packet, fn); + this.constructor.send(this.latus, this.socket.to(room), packet, fn); } }