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,9 +48,7 @@ class LagoonAddons {
|
||||
console.log('Lagoon loaded !');
|
||||
}
|
||||
async clock() {
|
||||
this.timer++;
|
||||
// 1 seconde
|
||||
if (this.timer % interval === 0) {
|
||||
this.timer += 20;
|
||||
if (this.ws.isOpen) {
|
||||
const players = world.getAllPlayers().filter((p) => p.initialized).map((p) => {
|
||||
return {
|
||||
@@ -58,6 +64,5 @@ class LagoonAddons {
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
new LagoonAddons();
|
||||
|
||||
36
src/main.ts
36
src/main.ts
@@ -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,13 +61,10 @@ class LagoonAddons {
|
||||
|
||||
|
||||
async clock() {
|
||||
this.timer++;
|
||||
|
||||
// 1 seconde
|
||||
if (this.timer % interval === 0) {
|
||||
this.timer += 20;
|
||||
|
||||
if (this.ws.isOpen) {
|
||||
const players: {x: number, y: number, z: number, code: string, muted: boolean}[] = world.getAllPlayers().filter((p) => p.initialized).map((p) => {
|
||||
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,
|
||||
@@ -70,7 +78,7 @@ class LagoonAddons {
|
||||
data: players
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user