From 1216a517b7f725c787609db453eaa48f99c92918 Mon Sep 17 00:00:00 2001 From: Adam Hovorka Date: Wed, 17 Jul 2019 23:23:52 -0600 Subject: Flesh out the WebSocket system with some amenities --- public/main.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'public/main.js') diff --git a/public/main.js b/public/main.js index b3c7986..46d5432 100644 --- a/public/main.js +++ b/public/main.js @@ -2,9 +2,15 @@ document.addEventListener("DOMContentLoaded", async () => { "use strict" const $ = s => document.querySelector(s); const secure = location.protocol === "https:"; -const sock = new WebSocket(`ws${secure?"s":""}://${location.host}/ws`); +sock.init(`ws${secure?"s":""}://${location.host}/ws`); -sock.onmessage = e => console.log("sock", e); -sock.onopen = () => sock.send("yay"); +sock.on("hello", e => { + console.log("hello", e); + sock.send("world", {foo:"bar"}); +}); + +sock.on("yay", e => { + console.log("yay", e); +}); }); -- cgit v1.2.3-54-g00ecf