2024-07-21 02:57:29 -05:00
|
|
|
import Client from '@/net/client.js';
|
2024-08-29 15:43:50 -05:00
|
|
|
import {decode, encode} from '@/net/packets/index.js';
|
2024-09-05 07:15:55 -05:00
|
|
|
import {CLIENT_INTERPOLATION, CLIENT_PREDICTION} from '@/util/constants.js';
|
2024-06-10 22:42:30 -05:00
|
|
|
|
|
|
|
export default class RemoteClient extends Client {
|
2024-08-29 15:43:50 -05:00
|
|
|
socket = null;
|
|
|
|
interpolator = null;
|
2024-09-05 07:15:55 -05:00
|
|
|
predictor = null;
|
2024-06-10 22:42:30 -05:00
|
|
|
async connect(host) {
|
2024-08-29 15:43:50 -05:00
|
|
|
this.interpolator = new Worker(
|
|
|
|
new URL('./interpolator.js', import.meta.url),
|
|
|
|
{type: 'module'},
|
|
|
|
);
|
2024-09-05 07:15:55 -05:00
|
|
|
if (CLIENT_INTERPOLATION) {
|
|
|
|
this.interpolator = new Worker(
|
|
|
|
new URL('./interpolator.js', import.meta.url),
|
|
|
|
{type: 'module'},
|
|
|
|
);
|
|
|
|
this.interpolator.addEventListener('message', (event) => {
|
|
|
|
this.accept(event.data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (CLIENT_PREDICTION) {
|
|
|
|
this.predictor = new Worker(
|
|
|
|
new URL('./predictor.js', import.meta.url),
|
|
|
|
{type: 'module'},
|
|
|
|
);
|
|
|
|
this.predictor.addEventListener('message', (event) => {
|
|
|
|
const [flow, packet] = event.data;
|
|
|
|
switch (flow) {
|
|
|
|
case 0: {
|
|
|
|
const packed = encode(packet);
|
|
|
|
this.throughput.$$up += packed.byteLength;
|
|
|
|
this.socket.send(packed);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
if (CLIENT_INTERPOLATION) {
|
|
|
|
this.interpolator.postMessage(packet);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.accept(packet);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-08-29 15:43:50 -05:00
|
|
|
const url = new URL(`wss://${host}/ws`)
|
|
|
|
this.socket = new WebSocket(url.href);
|
|
|
|
this.socket.binaryType = 'arraybuffer';
|
|
|
|
this.socket.addEventListener('message', (event) => {
|
2024-09-05 07:15:55 -05:00
|
|
|
this.throughput.$$down += event.data.byteLength;
|
|
|
|
const packet = decode(event.data);
|
|
|
|
if (CLIENT_PREDICTION) {
|
|
|
|
this.predictor.postMessage([1, packet]);
|
|
|
|
}
|
|
|
|
else if (CLIENT_INTERPOLATION) {
|
|
|
|
this.interpolator.postMessage(packet);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.accept(packet);
|
|
|
|
}
|
2024-08-29 15:43:50 -05:00
|
|
|
});
|
|
|
|
this.socket.addEventListener('close', () => {
|
|
|
|
this.accept({type: 'ConnectionStatus', payload: 'aborted'});
|
|
|
|
});
|
|
|
|
this.socket.addEventListener('error', () => {
|
|
|
|
this.accept({type: 'ConnectionStatus', payload: 'aborted'});
|
|
|
|
});
|
|
|
|
this.accept({type: 'ConnectionStatus', payload: 'connected'});
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
disconnect() {
|
2024-09-05 07:15:55 -05:00
|
|
|
if (CLIENT_INTERPOLATION) {
|
|
|
|
this.interpolator.terminate();
|
|
|
|
}
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
2024-08-29 15:43:50 -05:00
|
|
|
transmit(packet) {
|
2024-09-05 07:15:55 -05:00
|
|
|
if (CLIENT_PREDICTION) {
|
|
|
|
this.predictor.postMessage([0, packet]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const packed = encode(packet);
|
|
|
|
this.throughput.$$up += packed.byteLength;
|
|
|
|
this.socket.send(packed);
|
|
|
|
}
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
}
|