Skip to content

Commit

Permalink
improve url builder for Discord Activities
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Aug 29, 2024
1 parent 88a0ae2 commit 43d8a97
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "colyseus.js",
"version": "0.15.25",
"version": "0.15.26",
"description": "Colyseus Multiplayer SDK for JavaScript/TypeScript",
"author": "Endel Dreyer",
"license": "MIT",
Expand Down
42 changes: 33 additions & 9 deletions src/3rd_party/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,46 @@
* https://github.com/colyseus/colyseus/issues/707
*
* All URLs must go through the local proxy from
* https://<app_id>.discordsays.com/<backend>/...
* https://<app_id>.discordsays.com/.proxy/<mapped_url>/...
*
* You must configure your URL Mappings with:
* - /colyseus/{subdomain} -> {subdomain}.colyseus.cloud
* URL Mapping Examples:
*
* Example:
* const client = new Client("https://xxxx.colyseus.cloud");
* 1. Using Colyseus Cloud:
* - /colyseus/{subdomain} -> {subdomain}.colyseus.cloud
*
* Example:
* const client = new Client("https://xxxx.colyseus.cloud");
*
* -------------------------------------------------------------
*
* 2. Using `cloudflared` tunnel:
* - /colyseus/ -> <your-cloudflared-url>.trycloudflare.com
*
* Example:
* const client = new Client("https://<your-cloudflared-url>.trycloudflare.com");
*
* -------------------------------------------------------------
*
* 3. Providing a manual /.proxy/your-mapping:
* - /your-mapping/ -> your-endpoint.com
*
* Example:
* const client = new Client("/.proxy/your-mapping");
*
*/
export function discordURLBuilder (url: URL): string {
export function discordURLBuilder (url: URL): string {
const localHostname = window?.location?.hostname || "localhost";

const remoteHostnameSplitted = url.hostname.split('.');
const subdomain = (remoteHostnameSplitted.length > 2)
const subdomain = (
!url.hostname.includes("trycloudflare.com") && // ignore cloudflared subdomains
!url.hostname.includes("discordsays.com") && // ignore discordsays.com subdomains
remoteHostnameSplitted.length > 2
)
? `/${remoteHostnameSplitted[0]}`
: '';

return `${url.protocol}//${localHostname}/colyseus${subdomain}${url.pathname}${url.search}`;
}
return (url.pathname.startsWith("/.proxy"))
? `${url.protocol}//${localHostname}${subdomain}${url.pathname}${url.search}`
: `${url.protocol}//${localHostname}/.proxy/colyseus${subdomain}${url.pathname}${url.search}`;
}

0 comments on commit 43d8a97

Please sign in to comment.