Last active
May 13, 2024 16:37
-
-
Save Paradoxis/5f42ac638792a50fee1e82bd36450665 to your computer and use it in GitHub Desktop.
Convert WS/HTTP links with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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://"); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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://"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is REALLY REALLY useful
thanks