Skip to content

Instantly share code, notes, and snippets.

@Paradoxis
Last active May 13, 2024 16:37
Show Gist options
  • Save Paradoxis/5f42ac638792a50fee1e82bd36450665 to your computer and use it in GitHub Desktop.
Save Paradoxis/5f42ac638792a50fee1e82bd36450665 to your computer and use it in GitHub Desktop.
Convert WS/HTTP links with JavaScript
/**
* Converts an HTTP(S) url to a WS(S) URL
* Example:
* httpUrlToWebSockeUrl("http://www.example.com/") -> ws://www.example.com/
* httpUrlToWebSockeUrl("https://www.example.com/") -> wss://www.example.com/
*
* @param {string} url
* @return {string}
*/
function httpUrlToWebSockeUrl(url)
{
return url.replace(/(http)(s)?\:\/\//, "ws$2://");
}
/**
* Converts a WS(S) url to an HTTP(S) URL
* Example:
* webSocketUrlToHttpUrl("ws://www.example.com/") -> http://www.example.com/
* webSocketUrlToHttpUrl("wss://www.example.com/") -> https://www.example.com/
*
* @param {string} url
* @return {string}
*/
function webSocketUrlToHttpUrl(url)
{
return url.replace(/(ws)(s)?\:\/\//, "http$2://");
}
@C7TK
Copy link

C7TK commented May 13, 2024

this is REALLY REALLY useful
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment