32 lines
703 B
JavaScript
32 lines
703 B
JavaScript
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|