diff --git a/public_html/js/client.js b/public_html/js/client.js index 51b6869..b9cb761 100644 --- a/public_html/js/client.js +++ b/public_html/js/client.js @@ -10,10 +10,12 @@ * - receivedMessage: Chat message has been received. Data: object {from: 'username', text: 'text'} */ class Client extends EventDispatcher { - constructor(wsUri) { + constructor(wsUri, chatID, nickname) { super(); this.wsUri = wsUri; + this.chatID = chatID; + this.nickname = nickname; // Create WebSocket and set internal callbacks console.log("Initialize Client...") @@ -27,7 +29,7 @@ class Client extends EventDispatcher { // Internal WebSocket event handlers _onSocketOpen(evt) { console.log("Connected to " + this.wsUri); - this.sendInit(); + this.sendInit(this.chatID, this.nickname); } _onSocketClose(evt) { @@ -58,12 +60,15 @@ class Client extends EventDispatcher { /** * Sends the 'init' command which sets up the session. Also sets the chat ID and nickname. + * + * chatID: The ID of the chat instance. + * nickname: The user's nickname. */ - sendInit() { + sendInit(chatID, nickname) { this.sendCommand({ action: "init", - chat_id: "42", - nickname: "binaryDiv", + chat_id: chatID, + nickname: nickname, }); } @@ -81,6 +86,8 @@ class Client extends EventDispatcher { /** * Parses an incoming JSON message and dispatches specific events. + * + * msgText: The content of the message as a JSON string. */ _parseMessage(msgString) { try { diff --git a/public_html/js/ui.js b/public_html/js/ui.js index 63558ca..fb8cc99 100644 --- a/public_html/js/ui.js +++ b/public_html/js/ui.js @@ -15,7 +15,9 @@ class UI { this.initUI(); // TODO start client only after the user entered their nickname - this.initClient(); + let chatID = "42"; + let nickname = "binaryDiv"; + this.initClient(chatID, nickname); } /** @@ -27,10 +29,13 @@ class UI { /** * Create instance of Client and initialize connection. + * + * chatID: The ID of the chat instance. + * nickname: The user's nickname. */ - initClient() { + initClient(chatID, nickname) { const wsUri = AppSettings.serverWsUri; - this.client = new Client(wsUri); + this.client = new Client(wsUri, chatID, nickname); // Subscribe to Client events this.client.on("initialized", this._onClientInit.bind(this));