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();
|
||||
}
|
||||
|
||||
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);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user