aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstonedDiscord <Tukz@gmx.de>2023-12-02 01:40:06 +0100
committerGitHub <noreply@github.com>2023-12-02 01:40:06 +0100
commit8aa28d0fbd80210e3ee3086ed787ccf94c0c0803 (patch)
tree8441fbb9cd832461fcdf2dbcb00f9bf552741571
parent91a6fae8a3b69eaa3646aed88ed5e8de59bbf0a9 (diff)
parent6abdca42654d3eab390eb9cbe6cc91f6e2b12431 (diff)
Merge pull request #203 from Troid-Tech/adjust-connect-timeout
Add connect timeout
-rw-r--r--webAO/client.ts22
-rw-r--r--webAO/packets/handlers/handleDONE.ts15
2 files changed, 31 insertions, 6 deletions
diff --git a/webAO/client.ts b/webAO/client.ts
index 8a98c4b..50c5363 100644
--- a/webAO/client.ts
+++ b/webAO/client.ts
@@ -99,6 +99,14 @@ fpPromise
export const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));
+export enum clientState {
+ NotConnected,
+ // Should be set once the client has established a connection
+ Connected,
+ // Should be set once the client has joined the server (after handshake)
+ Joined
+}
+
export let lastICMessageTime = new Date(0);
export const setLastICMessageTime = (val: Date) => {
lastICMessageTime = val
@@ -130,12 +138,14 @@ class Client extends EventEmitter {
viewport: Viewport;
partial_packet: boolean;
temp_packet: string;
+ state: clientState;
connect: () => void;
loadResources: () => void
isLowMemory: () => void
constructor(connectionString: string) {
super();
+ this.state = clientState.NotConnected;
this.connect = () => {
this.on("open", this.onOpen.bind(this));
this.on("close", this.onClose.bind(this));
@@ -148,6 +158,14 @@ class Client extends EventEmitter {
this.serv.addEventListener("close", this.emit.bind(this, "close"));
this.serv.addEventListener("message", this.emit.bind(this, "message"));
this.serv.addEventListener("error", this.emit.bind(this, "error"));
+
+ // If the client is still not connected 5 seconds after attempting to join
+ // It's fair to assume that the server is not reachable
+ setTimeout(() => {
+ if (this.state === clientState.NotConnected) {
+ this.serv.close();
+ }
+ }, 5000);
} else {
this.joinServer();
}
@@ -234,6 +252,7 @@ class Client extends EventEmitter {
* Triggered when a connection is established to the server.
*/
onOpen(_e: Event) {
+ client.state = clientState.Connected;
client.joinServer();
}
@@ -242,6 +261,7 @@ class Client extends EventEmitter {
* @param {CloseEvent} e
*/
onClose(e: CloseEvent) {
+ client.state = clientState.NotConnected;
console.error(`The connection was closed: ${e.reason} (${e.code})`);
if (extrafeatures.length == 0 && banned === false) {
document.getElementById("client_errortext").textContent =
@@ -322,7 +342,9 @@ class Client extends EventEmitter {
* @param {ErrorEvent} e
*/
onError(e: ErrorEvent) {
+ client.state = clientState.NotConnected;
console.error(`A network error occurred`);
+ console.error(e);
document.getElementById("client_error").style.display = "flex";
this.cleanup();
}
diff --git a/webAO/packets/handlers/handleDONE.ts b/webAO/packets/handlers/handleDONE.ts
index 6ca31bf..e323986 100644
--- a/webAO/packets/handlers/handleDONE.ts
+++ b/webAO/packets/handlers/handleDONE.ts
@@ -1,4 +1,5 @@
import queryParser from "../../utils/queryParser";
+import { client, clientState } from "../../client";
const { mode } = queryParser()
/**
@@ -8,9 +9,11 @@ const { mode } = queryParser()
* @param {Array} args packet arguments
*/
export const handleDONE = (_args: string[]) => {
- document.getElementById("client_loading")!.style.display = "none";
- if (mode === "watch") {
- // Spectators don't need to pick a character
- document.getElementById("client_waiting")!.style.display = "none";
- }
-} \ No newline at end of file
+ // DONE packet signals that the handshake is complete
+ client.state = clientState.Joined;
+ document.getElementById("client_loading")!.style.display = "none";
+ if (mode === "watch") {
+ // Spectators don't need to pick a character
+ document.getElementById("client_waiting")!.style.display = "none";
+ }
+}