feat: dehydrated packet sending
This commit is contained in:
parent
1f9f53fe3c
commit
bb8f985bac
|
@ -77,21 +77,35 @@ export default class SocketClient extends decorate(Class) {
|
||||||
this.socket.disconnect();
|
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() {
|
get id() {
|
||||||
return this.socket ? this.socket.id : undefined;
|
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;
|
const {id} = packet.constructor;
|
||||||
debug('sending packet %s(%o)', packet.constructor.name, packet.data);
|
socket.binary(true).emit(id, packet.data, fn);
|
||||||
this.socket.binary(true).emit(id, packet.data, fn);
|
}
|
||||||
|
|
||||||
|
send(packet, fn) {
|
||||||
|
this.constructor.send(this.latus, this.socket, packet, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
to(channel) {
|
to(channel) {
|
||||||
return {
|
return {
|
||||||
send: (packet) => {
|
send: (packet, fn) => {
|
||||||
const {id} = packet.constructor;
|
this.constructor.send(this.latus, this.socket.to(channel), packet, fn);
|
||||||
this.socket.binary(true).to(channel).emit(id, packet.data);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ export default class ServerSocket extends decorate(Class) {
|
||||||
|
|
||||||
constructor(socket, latus) {
|
constructor(socket, latus) {
|
||||||
super();
|
super();
|
||||||
|
this.latus = latus;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
const Packets = Object.entries(all(latus));
|
const Packets = Object.entries(all(latus));
|
||||||
for (let i = 0; i < Packets.length; i++) {
|
for (let i = 0; i < Packets.length; i++) {
|
||||||
|
@ -37,6 +38,14 @@ export default class ServerSocket extends decorate(Class) {
|
||||||
this.socket.disconnect(true);
|
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() {
|
get id() {
|
||||||
return this.socket.id;
|
return this.socket.id;
|
||||||
}
|
}
|
||||||
|
@ -57,19 +66,22 @@ export default class ServerSocket extends decorate(Class) {
|
||||||
return this.socket.rooms;
|
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);
|
debug('sending packet %o', packet);
|
||||||
const {id} = packet.constructor;
|
const {id} = packet.constructor;
|
||||||
socket.binary(true).emit(id, packet.data, fn);
|
socket.binary(true).emit(id, packet.data, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
send(packet, fn) {
|
send(packet, fn) {
|
||||||
this.constructor.send(this.socket, packet, fn);
|
this.constructor.send(this.latus, this.socket, packet, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
to(room, packet, fn) {
|
to(room, packet, fn) {
|
||||||
debug('broadcasting to %s', room);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user