Refactor connection handling and improve reconnection logic in LagoonAddons
This commit is contained in:
@@ -2,13 +2,13 @@ import { system, world } from "@minecraft/server";
|
||||
import utils from "utils";
|
||||
import { websocket } from "@minecraft/server-net";
|
||||
import { InitPlayer } from "entities/player";
|
||||
import { interval, roomCode } from "properties";
|
||||
import { roomCode } from "properties";
|
||||
class LagoonAddons {
|
||||
constructor() {
|
||||
this.timer = 0;
|
||||
world.afterEvents.worldLoad.subscribe(() => system.run(() => this.initEarlyExecution()));
|
||||
}
|
||||
async initEarlyExecution() {
|
||||
async connect() {
|
||||
this.ws = await websocket.connect('wss://lagoon.under-scape.com/ws/rooms/' + roomCode + '/plugins/proximity');
|
||||
this.ws.afterEvents.message.subscribe((event) => {
|
||||
try {
|
||||
@@ -26,9 +26,17 @@ class LagoonAddons {
|
||||
}
|
||||
});
|
||||
this.ws.afterEvents.close.subscribe((event) => {
|
||||
utils.logStaff(`§cConnexion à lagoon fermé ! (${event})`);
|
||||
console.log(`Connexion à lagoon fermé ! (${event})`);
|
||||
utils.logStaff(`§cConnection to Lagoon closed. (${event})`);
|
||||
console.log(`Connection to Lagoon closed. (${event})`);
|
||||
utils.logStaff(`§eReconnecting in 5 seconds.`);
|
||||
console.log(`Reconnecting in 5 seconds.`);
|
||||
system.runTimeout(() => {
|
||||
this.connect();
|
||||
}, 20 * 5);
|
||||
});
|
||||
}
|
||||
async initEarlyExecution() {
|
||||
await this.connect();
|
||||
world.getAllPlayers().forEach((p) => InitPlayer(p));
|
||||
world.afterEvents.playerSpawn.subscribe((e) => {
|
||||
system.runTimeout(() => {
|
||||
@@ -40,23 +48,20 @@ class LagoonAddons {
|
||||
console.log('Lagoon loaded !');
|
||||
}
|
||||
async clock() {
|
||||
this.timer++;
|
||||
// 1 seconde
|
||||
if (this.timer % interval === 0) {
|
||||
if (this.ws.isOpen) {
|
||||
const players = world.getAllPlayers().filter((p) => p.initialized).map((p) => {
|
||||
return {
|
||||
...p.location,
|
||||
code: p.lagoonCode,
|
||||
muted: p.hasTag('muted')
|
||||
};
|
||||
});
|
||||
this.ws.send(JSON.stringify({
|
||||
type: 'positions',
|
||||
uuid: utils.makeid(100),
|
||||
data: players
|
||||
}));
|
||||
}
|
||||
this.timer += 20;
|
||||
if (this.ws.isOpen) {
|
||||
const players = world.getAllPlayers().filter((p) => p.initialized).map((p) => {
|
||||
return {
|
||||
...p.location,
|
||||
code: p.lagoonCode,
|
||||
muted: p.hasTag('muted')
|
||||
};
|
||||
});
|
||||
this.ws.send(JSON.stringify({
|
||||
type: 'positions',
|
||||
uuid: utils.makeid(100),
|
||||
data: players
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user