Refactor connection handling and improve reconnection logic in LagoonAddons

This commit is contained in:
2026-05-27 11:17:42 +02:00
parent 7ba6587f02
commit 9fb87dd3e0
2 changed files with 60 additions and 47 deletions

View File

@@ -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
}));
}
}
}

View File

@@ -9,31 +9,42 @@ class LagoonAddons {
private ws!: WebSocketClient;
public timer: number = 0;
constructor () {
constructor() {
world.afterEvents.worldLoad.subscribe(() => system.run(() => this.initEarlyExecution()));
}
async initEarlyExecution() {
this.ws = await websocket.connect('wss://lagoon.under-scape.com/ws/rooms/'+roomCode+'/plugins/proximity');
async connect() {
this.ws = await websocket.connect('wss://lagoon.under-scape.com/ws/rooms/' + roomCode + '/plugins/proximity');
this.ws.afterEvents.message.subscribe((event) => {
try {
const payload = JSON.parse(event.message);
const type = payload.type;
const data = payload.data;
if(type == 'status' && data == 'connected !') {
if (type == 'status' && data == 'connected !') {
utils.logStaff('§aLagoon connected !');
console.log('Lagoon connected !')
}
} catch (error) {
utils.logStaff('ERROR With Payload : '+event.message);
console.log('ERROR With Payload : '+event.message)
utils.logStaff('ERROR With Payload : ' + event.message);
console.log('ERROR With Payload : ' + event.message)
}
});
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) => {
@@ -50,27 +61,24 @@ class LagoonAddons {
async clock() {
this.timer++;
this.timer += 20;
// 1 seconde
if (this.timer % interval === 0) {
if (this.ws.isOpen) {
const players: { x: number, y: number, z: number, code: string, muted: boolean }[] = world.getAllPlayers().filter((p) => p.initialized).map((p) => {
return {
...p.location,
code: p.lagoonCode,
muted: p.hasTag('muted')
}
});
if (this.ws.isOpen) {
const players: {x: number, y: number, z: number, code: string, muted: boolean}[] = 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.ws.send(JSON.stringify({
type: 'positions',
uuid: utils.makeid(100),
data: players
}));
}
}
}