Add setting UI for port/reconnect, swap wrong API handlers

This commit is contained in:
Zoë Hoekstra
2022-07-31 19:53:15 +02:00
parent d20f621b47
commit 5fc67c328b
7 changed files with 127 additions and 52 deletions

View File

@@ -77,26 +77,12 @@ export const RFARequestHandler: Record<string, (message: RFAMessage) => void | R
return new RFAMessage({ result: "OK", id: msg.id });
},
getFileNames: function (msg: RFAMessage): RFAMessage {
getFileNames: function (msg: RFAMessage): RFAMessage {
if (!isFileServer(msg.params)) return error("getFileNames message misses parameters", msg);
const server = GetServer(msg.params.server);
if (server == null) return error("Server hostname invalid", msg);
const fileList: FileContent[] = [
...server.textFiles.map((txt): FileContent => { return { filename: txt.filename, content: txt.text } }),
...server.scripts.map((scr): FileContent => { return { filename: scr.filename, content: scr.code } })
];
return new RFAMessage({ result: JSON.stringify(fileList), id: msg.id });
},
getAllFiles: function (msg: RFAMessage): RFAMessage {
if (!isFileServer(msg.params)) return error("getAllFiles message misses parameters", msg);
const server = GetServer(msg.params.server);
if (server == null) return error("Server hostname invalid", msg);
const fileNameList: string[] = [
...server.textFiles.map((txt): string => txt.filename),
...server.scripts.map((scr): string => scr.filename)
@@ -105,6 +91,20 @@ export const RFARequestHandler: Record<string, (message: RFAMessage) => void | R
return new RFAMessage({ result: JSON.stringify(fileNameList), id: msg.id });
},
getAllFiles: function (msg: RFAMessage): RFAMessage {
if (!isFileServer(msg.params)) return error("getAllFiles message misses parameters", msg);
const server = GetServer(msg.params.server);
if (server == null) return error("Server hostname invalid", msg);
const fileList: FileContent[] = [
...server.textFiles.map((txt): FileContent => { return { filename: txt.filename, content: txt.text } }),
...server.scripts.map((scr): FileContent => { return { filename: scr.filename, content: scr.code } })
];
return new RFAMessage({ result: JSON.stringify(fileList), id: msg.id });
},
calculateRam: function (msg: RFAMessage): RFAMessage {
if (!isFileLocation(msg.params)) return error("calculateRam message misses parameters", msg);
const fileData: FileLocation = msg.params;

View File

@@ -13,36 +13,18 @@ export class Remote {
this.port = port;
}
public stopConnection() : void {
this.connection?.close();
}
public startConnection() : void {
this.startConnectionRecurse(1, 5);
}
handleCloseEvent():void {
delete this.connection;
RFALogger.log("Connection closed.");
}
startConnectionRecurse(retryN : number, retryMax : number) : void {
RFALogger.log("Trying to connect.");
this.connection = new WebSocket(this.protocol + "://" + this.ipaddr + ":" + this.port);
this.connection.addEventListener("error", (e:Event) => {
if(!this.connection) return;
// When having trouble connecting, try again.
if (this.connection.readyState === 3) {
RFALogger.log(`Connection lost, retrying (try #${retryN}).`);
if (retryN <= retryMax) this.startConnectionRecurse(retryN + 1, retryMax);
return;
}
// Else handle the error normally
RFALogger.error(e);
});
this.connection.addEventListener("error", (e:Event) => RFALogger.error(e));
this.connection.addEventListener("message", handleMessageEvent);
this.connection.addEventListener("open", () => RFALogger.log("Connection established: ", this.ipaddr, ":", this.port));
this.connection.addEventListener("close", this.handleCloseEvent);
this.connection.addEventListener("close", () => RFALogger.log("Connection closed"));
}
}

View File

@@ -1,16 +1,19 @@
import { Settings } from "../Settings/Settings";
import { Remote } from "./Remote";
class RemoteFileAPI {
server : Remote;
constructor(){
this.server = new Remote("localhost", 12525);
return;
}
let server: Remote;
enable() : void {
this.server.startConnection();
export function newRemoteFileApiConnection() : void {
if(server == undefined)
server = new Remote("localhost", Settings.RemoteFileApiPort);
else {
server.stopConnection();
server = new Remote("localhost", Settings.RemoteFileApiPort);
server.startConnection();
}
}
export const RFA = new RemoteFileAPI;
export function isRemoteFileApiConnectionLive() : boolean {
return server.connection != undefined && server.connection.readyState == 1;
}