Compare commits

..

No commits in common. "98c89f0702374d7883eb09673d058acb9aa35654" and "fd6a40cb0d3931847df6b7fd5c9ed260dc3122eb" have entirely different histories.

3 changed files with 69 additions and 73 deletions

View File

@ -11,7 +11,6 @@
<h1>InstantChat</h1> <h1>InstantChat</h1>
<script src="js/settings.js"></script>
<script src="js/client.js"></script> <script src="js/client.js"></script>
</body> </body>

View File

@ -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 // Initialization
console.log("Initialize Client...") function init() {
this.webSocket = new WebSocket(wsUri); console.log("Init...");
this.webSocket.onopen = this.onOpen.bind(this); openWebSocket();
this.webSocket.onclose = this.onClose.bind(this); }
this.webSocket.onmessage = this.onMessage.bind(this);
this.webSocket.onerror = this.onError.bind(this);
}
// WebSocket event handlers // Open WebSocket
onOpen(evt) { function openWebSocket() {
console.log("Connected to " + this.wsUri); 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 // WebSocket event handlers
this.sendInit(); function onOpen(evt) {
} console.log("Connected to " + wsUri + ".");
onClose(evt) { // Send init command containing chat ID and nickname
console.log("Connection closed (code " + evt.code + ")."); sendInit();
} }
onMessage(evt) { function onClose(evt) {
console.log("Received: " + evt.data); console.log("Connection closed (code " + evt.code + ").");
this.parseMessage(evt.data); }
}
onError(evt) { function onMessage(evt) {
console.error("Connection error: ", evt); console.log("Received: " + evt.data);
} parseMessage(evt.data);
}
// Command senders function onError(evt) {
sendInit() { console.error("Connection error: ", evt);
// Define command as JSON object }
let initObj = {
action: "init",
chat_id: "42",
nickname: "binaryDiv"
};
// Send command as JSON string // Command senders
let initJson = JSON.stringify(initObj); function sendInit() {
console.log("Sending init: " + initJson); // Define command as JSON object
this.webSocket.send(initJson); let initObj = {
} action: "init",
chat_id: "42",
nickname: "binaryDiv"
};
// Message parsing // Send command as JSON string
parseMessage(msgString) { let initJson = JSON.stringify(initObj);
try { console.log("Sending init: " + initJson);
let msg = JSON.parse(msgString); websocket.send(initJson);
}
switch (msg.type) { // Message parsing
// Response to "init" command function parseMessage(msgString) {
case "init": try {
// TODO let msg = JSON.parse(msgString);
console.log("Got init response: ", msg);
break;
// Incoming chat message switch (msg.type) {
case "message": // Response to "init" command
// TODO case "init":
console.log("Got message event: from '" + msg.from + "', text '" + msg.text + "'"); // TODO
break; 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: // TODO Topic change, user join/leave, error, ...
console.error("Unknown message type '" + msg.type + "'");
} default:
} console.error("Unknown message type '" + msg.type + "'");
catch (e) {
console.error("Error parsing message JSON: " + e.message);
} }
} }
catch (e) {
console.error("Error parsing message JSON: " + e.message);
}
} }
// Run script after page is loaded // Run script after page is loaded
$(function() { $(function() {
const wsUri = AppSettings.serverWsUri; init();
let client = new Client(wsUri);
}); });

View File

@ -1,7 +0,0 @@
"use strict";
// Global settings object
const AppSettings = {
// serverWsUri: "ws://localhost:32715",
serverWsUri: "wss://chat.glitch-in.space:443/ws/",
};