silphius/app/net/client.js
2024-07-21 02:57:29 -05:00

28 lines
642 B
JavaScript

import {CLIENT_LATENCY} from '@/util/constants.js';
import EventEmitter from '@/util/event-emitter.js';
export default class Client {
constructor() {
this.emitter = new EventEmitter();
}
accept(packet) {
this.emitter.invoke(packet.type, packet.payload);
}
addPacketListener(type, listener) {
this.emitter.addListener(type, listener);
}
removePacketListener(type, listener) {
this.emitter.removeListener(type, listener);
}
send(packet) {
if (CLIENT_LATENCY > 0) {
setTimeout(() => {
this.transmit(packet);
}, CLIENT_LATENCY);
}
else {
this.transmit(packet);
}
}
}