Created
September 7, 2022 13:51
-
-
Save percybolmer/5d88de120dd365d0dc77a3570a00ed8e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Once the website loads, we want to apply listeners and connect to websocket | |
* */ | |
window.onload = function () { | |
// Apply our listener functions to the submit event on both forms | |
// we do it this way to avoid redirects | |
document.getElementById("chatroom-selection").onsubmit = changeChatRoom; | |
document.getElementById("chatroom-message").onsubmit = sendMessage; | |
// Check if the browser supports WebSocket | |
if (window["WebSocket"]) { | |
console.log("supports websockets"); | |
// Connect to websocket | |
conn = new WebSocket("ws://" + document.location.host + "/ws"); | |
// Add a listener to the onmessage event | |
conn.onmessage = function(evt) { | |
console.log(evt); | |
} | |
} else { | |
alert("Not supporting websockets"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment