Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created September 7, 2022 13:51
Show Gist options
  • Save percybolmer/5d88de120dd365d0dc77a3570a00ed8e to your computer and use it in GitHub Desktop.
Save percybolmer/5d88de120dd365d0dc77a3570a00ed8e to your computer and use it in GitHub Desktop.
/**
* 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