function status_update(ev) { console.log("Got message from server: " + ev.data); } function ws_connected(ev) { console.log("Connection to fetapi now active!"); ev.target.onmessage = status_update; ev.target.send("test"); } function connect_ws() { webSocket = new WebSocket("ws://" + window.location.hostname + ":" + window.location.port + "/ws"); webSocket.onopen = ws_connected; } function init() { connect_ws(); } document.addEventListener("DOMContentLoaded", init);