Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connect timeout #203

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions webAO/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,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
Expand Down Expand Up @@ -129,12 +137,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));
Expand All @@ -147,6 +157,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();
}
Expand Down Expand Up @@ -227,6 +245,7 @@ class Client extends EventEmitter {
* Triggered when a connection is established to the server.
*/
onOpen(_e: Event) {
client.state = clientState.Connected;
client.joinServer();
}

Expand All @@ -235,6 +254,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 =
Expand Down Expand Up @@ -315,7 +335,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();
}
Expand Down
15 changes: 9 additions & 6 deletions webAO/packets/handlers/handleDONE.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import queryParser from "../../utils/queryParser";
import { client, clientState } from "../../client";

const { mode } = queryParser()
/**
Expand All @@ -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";
}
}
// 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";
}
}
Loading