silphius/app/net/client.js
2024-08-06 00:40:43 -05:00

35 lines
895 B
JavaScript

import {CLIENT_LATENCY} from '@/util/constants.js';
import EventEmitter from '@/util/event-emitter.js';
export default class Client {
emitter = new EventEmitter();
throughput = {$$down: 0, down: 0, $$up: 0, up: 0};
constructor() {
setInterval(() => {
const {throughput} = this;
throughput.down = throughput.$$down * 4;
throughput.up = throughput.$$up * 4;
throughput.$$down = throughput.$$up = 0;
}, 250);
}
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);
}
}
}