instantchat/public_html/js/ui.js

73 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
2019-02-24 15:11:35 +01:00
// Global objects for debugging purposes
let client;
2019-02-24 15:11:35 +01:00
let ui;
2019-02-24 15:11:35 +01:00
/**
* Class for handling the UI.
*/
class UI {
constructor() {
this.client = null;
// Initialize the UI
this.initUI();
// TODO start client only after the user entered their nickname
let chatID = "42";
let nickname = "binaryDiv";
this.initClient(chatID, nickname);
2019-02-24 15:11:35 +01:00
}
/**
* Initialize the web UI.
*/
initUI() {
// TODO stub
}
/**
* Create instance of Client and initialize connection.
*
* chatID: The ID of the chat instance.
* nickname: The user's nickname.
2019-02-24 15:11:35 +01:00
*/
initClient(chatID, nickname) {
2019-02-24 15:11:35 +01:00
const wsUri = AppSettings.serverWsUri;
this.client = new Client(wsUri, chatID, nickname);
2019-02-24 15:11:35 +01:00
// Subscribe to Client events
this.client.on("initialized", this._onClientInit.bind(this));
this.client.on("disconnected", this._onClientDisconnect.bind(this));
this.client.on("connectionError", this._onClientError.bind(this));
this.client.on("receivedMessage", this._onClientReceivedMessage.bind(this));
}
2019-01-20 21:12:41 +01:00
2019-02-24 15:11:35 +01:00
_onClientInit() {
2019-01-20 21:12:41 +01:00
console.log("UI: Connection initialized!");
// Send a test message
2019-02-24 15:11:35 +01:00
this.client.sendChatMessage("Meow meow! :3");
}
_onClientDisconnect() {
2019-01-20 21:12:41 +01:00
console.log("UI: Connection closed!");
2019-02-24 15:11:35 +01:00
}
_onClientError() {
console.log("UI: Connection error! :(");
}
_onClientReceivedMessage(msg) {
2019-01-20 21:12:41 +01:00
console.log("UI: Message from '" + msg.from + "', text: '" + msg.text + "'");
2019-02-24 15:11:35 +01:00
}
}
// Execute this on start (wrapped in an anonymous function)
(function() {
// TODO
ui = new UI();
client = ui.client;
})();