silphius/app/net/server/server.js

32 lines
703 B
JavaScript
Raw Normal View History

2024-06-10 22:42:30 -05:00
import {SERVER_LATENCY} from '@/constants.js';
export default class Server {
constructor() {
this.listeners = [];
}
accept(connection, packet) {
for (const i in this.listeners) {
this.listeners[i](connection, packet);
}
}
addPacketListener(listener) {
this.listeners.push(listener);
}
removePacketListener(listener) {
const index = this.listeners.indexOf(listener);
if (-1 !== index) {
this.listeners.splice(index, 1);
}
}
send(connection, packet) {
if (SERVER_LATENCY > 0) {
setTimeout(() => {
this.transmit(connection, packet);
}, SERVER_LATENCY);
}
else {
this.transmit(connection, packet);
}
}
}