updates fresh, adds socket error handling
This commit is contained in:
@@ -12,6 +12,7 @@ type status = "running" | "stopped";
|
||||
|
||||
class ServerState {
|
||||
private _status: status = "stopped";
|
||||
private pid?: number;
|
||||
public get status() {
|
||||
return this._status;
|
||||
}
|
||||
@@ -65,26 +66,31 @@ class ServerState {
|
||||
this._serverVersion = conf.version;
|
||||
|
||||
this._eulaAccepted = checkEULA();
|
||||
}
|
||||
|
||||
this.sockpuppet = new Sockpuppet(
|
||||
"ws://sockpuppet.cyborggrizzly.com",
|
||||
() => {
|
||||
this.sockpuppet.createChannel(this._channelId);
|
||||
this.sockpuppet.joinChannel(this.channelId, async (msg) => {
|
||||
if (msg === "log" && !IS_BROWSER) {
|
||||
try {
|
||||
const log = await Deno.readTextFile("./server/logs/latest.log");
|
||||
this.channel?.send(log);
|
||||
} catch {
|
||||
1;
|
||||
private configureSockpuppet(): Promise<boolean> {
|
||||
return new Promise((res) => {
|
||||
this.sockpuppet = new Sockpuppet(
|
||||
"ws://sockpuppet.cyborggrizzly.com",
|
||||
async () => {
|
||||
await this.sockpuppet.createChannel(this._channelId);
|
||||
res(true);
|
||||
this.sockpuppet.joinChannel(this.channelId, async (msg) => {
|
||||
if (msg === "log" && !IS_BROWSER) {
|
||||
try {
|
||||
const log = await Deno.readTextFile("./server/logs/latest.log");
|
||||
this.sendMessageToChannel(log);
|
||||
} catch {
|
||||
1;
|
||||
}
|
||||
} else {
|
||||
console.log(msg);
|
||||
this.sendStdIn(msg);
|
||||
}
|
||||
} else {
|
||||
console.log(msg);
|
||||
this.sendStdIn(msg);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public get channel() {
|
||||
@@ -103,9 +109,12 @@ class ServerState {
|
||||
public async startMCServer(instance = "server") {
|
||||
this.command = getServerStartCommands(this._serverType, instance);
|
||||
|
||||
this.channel?.send("clear");
|
||||
await this.configureSockpuppet();
|
||||
|
||||
this.sendMessageToChannel("clear");
|
||||
|
||||
this.process = this.command.spawn();
|
||||
this.pid = this.process.pid;
|
||||
|
||||
const { readable, writable } = new TransformStream();
|
||||
readable.pipeTo(this.process.stdin);
|
||||
@@ -119,26 +128,10 @@ class ServerState {
|
||||
}
|
||||
|
||||
private async startStream() {
|
||||
// const stream = this.process.stdout.getReader();
|
||||
// const decoder = new TextDecoder();
|
||||
|
||||
// while (true) {
|
||||
// const { done, value } = await stream.read();
|
||||
// if (value) {
|
||||
// const line = decoder.decode(value);
|
||||
// this.channel?.send(line);
|
||||
// const stdoutMsg = new CustomEvent("stdoutmsg", {
|
||||
// detail: line,
|
||||
// });
|
||||
// globalThis.dispatchEvent(stdoutMsg);
|
||||
// }
|
||||
// if (done) break;
|
||||
// }
|
||||
|
||||
await this.process.stdout.pipeThrough(new TextDecoderStream()).pipeTo(
|
||||
new WritableStream({
|
||||
write: (chunk) => {
|
||||
this.channel?.send(chunk);
|
||||
this.sendMessageToChannel(chunk);
|
||||
const stdoutMsg = new CustomEvent("stdoutmsg", {
|
||||
detail: chunk,
|
||||
});
|
||||
@@ -153,9 +146,10 @@ class ServerState {
|
||||
this.sendStdIn("stop");
|
||||
}
|
||||
|
||||
public forceStopMCServer() {
|
||||
public async forceStopMCServer() {
|
||||
this.status = "stopped";
|
||||
this.process.kill();
|
||||
await Deno.remove('./server/session.lock');
|
||||
}
|
||||
|
||||
public async restartMCServer() {
|
||||
@@ -166,10 +160,7 @@ class ServerState {
|
||||
});
|
||||
|
||||
globalThis.dispatchEvent(statusEvent);
|
||||
|
||||
// while (true) {
|
||||
await this.process.status;
|
||||
// }
|
||||
}
|
||||
|
||||
this.startMCServer();
|
||||
@@ -179,6 +170,21 @@ class ServerState {
|
||||
this._eulaAccepted = true;
|
||||
acceptEULA();
|
||||
}
|
||||
|
||||
private async sendMessageToChannel(msg: string, retries = 0) {
|
||||
if (retries > 4) return console.log('Unable to reconnect to socket after 5 tries. Is the puppet server ok?')
|
||||
try {
|
||||
this.channel?.send(msg);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
console.log(
|
||||
"Error sending message via socket, likely closed. Reconnecting",
|
||||
);
|
||||
|
||||
await this.configureSockpuppet();
|
||||
await this.sendMessageToChannel(msg, retries + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const SERVER_STATE = new ServerState();
|
||||
|
Reference in New Issue
Block a user