diff options
| author | stonedDiscord <Tukz@gmx.de> | 2023-11-22 19:27:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-22 19:27:10 +0100 |
| commit | 26e3cd4bfe08a78e65935cf494c24193f59c8a7e (patch) | |
| tree | d5bc00fd748bceedb8693003916beed1f242ee3e /webAO/client.ts | |
| parent | 6480caf604d06778109cb58e5983d883725838a9 (diff) | |
| parent | f848f34b9979740e85b9648eb42ecafd20dd3926 (diff) | |
Merge pull request #201 from Troid-Tech/connect-to-wss
Fix https and add wss support
Diffstat (limited to 'webAO/client.ts')
| -rw-r--r-- | webAO/client.ts | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/webAO/client.ts b/webAO/client.ts index 3170ec8..c49a104 100644 --- a/webAO/client.ts +++ b/webAO/client.ts @@ -17,7 +17,7 @@ import { loadResources } from './client/loadResources' import { AO_HOST } from './client/aoHost' import { fetchBackgroundList, fetchEvidenceList, fetchCharacterList } from './client/fetchLists' -const { ip: serverIP, mode, theme, serverName } = queryParser(); +const { ip: serverIP, connect, mode, theme, serverName } = queryParser(); document.title = serverName; @@ -70,12 +70,27 @@ fpPromise .then((result) => { hdid = result.visitorId; - if (!serverIP) { - alert("No server IP specified!"); - return; + let connectionString = connect; + + if (!connectionString) { + if (serverIP) { + // if connectionString is not set, try IP + // and just guess ws, though it could be wss + connectionString = `ws://${serverIP}`; + } else { + alert("No connection string specified!"); + return; + } + } + + if (window.location.protocol === "https:" && connectionString.startsWith("ws://")) { + // If protocol is https: and connectionString is ws:// + // We have a problem, since it's impossible to connect to ws:// from https:// + // Connection will fail, but at least warn the user + alert('Attempted to connect using insecure websockets on https page. Please try removing s from https:// in the URL bar.') } - client = new Client(serverIP); + client = new Client(connectionString); client.connect() isLowMemory(); loadResources(); @@ -117,7 +132,7 @@ class Client extends EventEmitter { connect: () => void; loadResources: () => void isLowMemory: () => void - constructor(address: string) { + constructor(connectionString: string) { super(); this.connect = () => { @@ -126,7 +141,7 @@ class Client extends EventEmitter { this.on("message", this.onMessage.bind(this)); this.on("error", this.onError.bind(this)); if (mode !== "replay") { - this.serv = new WebSocket(`ws://${address}`); + this.serv = new WebSocket(connectionString); // Assign the websocket events this.serv.addEventListener("open", this.emit.bind(this, "open")); this.serv.addEventListener("close", this.emit.bind(this, "close")); |
