2024-08-07 14:29:05 -05:00
|
|
|
import {CLIENT_LATENCY, CLIENT_PREDICTION} from '@/util/constants.js';
|
2024-07-21 02:30:13 -05:00
|
|
|
import EventEmitter from '@/util/event-emitter.js';
|
2024-06-10 22:42:30 -05:00
|
|
|
|
|
|
|
export default class Client {
|
2024-08-06 00:40:43 -05:00
|
|
|
emitter = new EventEmitter();
|
2024-08-07 14:29:05 -05:00
|
|
|
rtt = 0;
|
2024-08-06 00:40:43 -05:00
|
|
|
throughput = {$$down: 0, down: 0, $$up: 0, up: 0};
|
2024-06-10 22:42:30 -05:00
|
|
|
constructor() {
|
2024-08-06 00:40:43 -05:00
|
|
|
setInterval(() => {
|
|
|
|
const {throughput} = this;
|
|
|
|
throughput.down = throughput.$$down * 4;
|
|
|
|
throughput.up = throughput.$$up * 4;
|
|
|
|
throughput.$$down = throughput.$$up = 0;
|
|
|
|
}, 250);
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
accept(packet) {
|
2024-08-07 14:29:05 -05:00
|
|
|
if ('Heartbeat' === packet.type) {
|
|
|
|
this.rtt = packet.payload.rtt;
|
|
|
|
this.send(packet);
|
|
|
|
return;
|
|
|
|
}
|
2024-07-21 02:30:13 -05:00
|
|
|
this.emitter.invoke(packet.type, packet.payload);
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
2024-06-13 01:26:01 -05:00
|
|
|
addPacketListener(type, listener) {
|
2024-07-21 02:30:13 -05:00
|
|
|
this.emitter.addListener(type, listener);
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
2024-06-13 01:26:01 -05:00
|
|
|
removePacketListener(type, listener) {
|
2024-07-21 02:30:13 -05:00
|
|
|
this.emitter.removeListener(type, listener);
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
send(packet) {
|
2024-08-07 14:29:05 -05:00
|
|
|
if (CLIENT_LATENCY > 0 && !CLIENT_PREDICTION) {
|
2024-06-10 22:42:30 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
this.transmit(packet);
|
|
|
|
}, CLIENT_LATENCY);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.transmit(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|