diff --git a/public_html/index.html b/public_html/index.html index c4cf468..bd0597e 100644 --- a/public_html/index.html +++ b/public_html/index.html @@ -11,7 +11,6 @@

InstantChat

- diff --git a/public_html/js/client.js b/public_html/js/client.js index 974a5bd..8e9752e 100644 --- a/public_html/js/client.js +++ b/public_html/js/client.js @@ -1,86 +1,90 @@ -"use strict"; +// Constants and variables +// const wsUri = "ws://localhost:32715"; +const wsUri = "wss://chat.glitch-in.space:443/ws/"; +let websocket; -class Client { - constructor(wsUri) { - this.wsUri = wsUri; - // Create WebSocket and set callbacks - console.log("Initialize Client...") - this.webSocket = new WebSocket(wsUri); - this.webSocket.onopen = this.onOpen.bind(this); - this.webSocket.onclose = this.onClose.bind(this); - this.webSocket.onmessage = this.onMessage.bind(this); - this.webSocket.onerror = this.onError.bind(this); - } +// Initialization +function init() { + console.log("Init..."); + openWebSocket(); +} - // WebSocket event handlers - onOpen(evt) { - console.log("Connected to " + this.wsUri); +// Open WebSocket +function openWebSocket() { + websocket = new WebSocket(wsUri); + websocket.onopen = function(evt) { onOpen(evt) }; + websocket.onclose = function(evt) { onClose(evt) }; + websocket.onmessage = function(evt) { onMessage(evt) }; + websocket.onerror = function(evt) { onError(evt) }; +} - // Send init command containing chat ID and nickname - this.sendInit(); - } +// WebSocket event handlers +function onOpen(evt) { + console.log("Connected to " + wsUri + "."); - onClose(evt) { - console.log("Connection closed (code " + evt.code + ")."); - } + // Send init command containing chat ID and nickname + sendInit(); +} - onMessage(evt) { - console.log("Received: " + evt.data); - this.parseMessage(evt.data); - } +function onClose(evt) { + console.log("Connection closed (code " + evt.code + ")."); +} - onError(evt) { - console.error("Connection error: ", evt); - } +function onMessage(evt) { + console.log("Received: " + evt.data); + parseMessage(evt.data); +} - // Command senders - sendInit() { - // Define command as JSON object - let initObj = { - action: "init", - chat_id: "42", - nickname: "binaryDiv" - }; +function onError(evt) { + console.error("Connection error: ", evt); +} - // Send command as JSON string - let initJson = JSON.stringify(initObj); - console.log("Sending init: " + initJson); - this.webSocket.send(initJson); - } +// Command senders +function sendInit() { + // Define command as JSON object + let initObj = { + action: "init", + chat_id: "42", + nickname: "binaryDiv" + }; - // Message parsing - parseMessage(msgString) { - try { - let msg = JSON.parse(msgString); + // Send command as JSON string + let initJson = JSON.stringify(initObj); + console.log("Sending init: " + initJson); + websocket.send(initJson); +} - switch (msg.type) { - // Response to "init" command - case "init": - // TODO - console.log("Got init response: ", msg); - break; +// Message parsing +function parseMessage(msgString) { + try { + let msg = JSON.parse(msgString); - // Incoming chat message - case "message": - // TODO - console.log("Got message event: from '" + msg.from + "', text '" + msg.text + "'"); - break; + switch (msg.type) { + // Response to "init" command + case "init": + // TODO + console.log("Got init response: ", msg); + break; - // TODO Topic change, user join/leave, error, ... + // Incoming chat message + case "message": + // TODO + console.log("Got message event: from '" + msg.from + "', text '" + msg.text + "'"); + break; - default: - console.error("Unknown message type '" + msg.type + "'"); - } - } - catch (e) { - console.error("Error parsing message JSON: " + e.message); + // TODO Topic change, user join/leave, error, ... + + default: + console.error("Unknown message type '" + msg.type + "'"); } } + catch (e) { + console.error("Error parsing message JSON: " + e.message); + } } // Run script after page is loaded $(function() { - const wsUri = AppSettings.serverWsUri; - let client = new Client(wsUri); + init(); }); diff --git a/public_html/js/settings.js b/public_html/js/settings.js deleted file mode 100644 index eec7552..0000000 --- a/public_html/js/settings.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -// Global settings object -const AppSettings = { - // serverWsUri: "ws://localhost:32715", - serverWsUri: "wss://chat.glitch-in.space:443/ws/", -};