Client: get chatID and nickname via parameters

This commit is contained in:
Lexi / Zoe 2019-02-24 16:53:25 +01:00
parent 3c9492d4b6
commit 23339dedce
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
2 changed files with 20 additions and 8 deletions

View File

@ -10,10 +10,12 @@
* - receivedMessage: Chat message has been received. Data: object {from: 'username', text: 'text'} * - receivedMessage: Chat message has been received. Data: object {from: 'username', text: 'text'}
*/ */
class Client extends EventDispatcher { class Client extends EventDispatcher {
constructor(wsUri) { constructor(wsUri, chatID, nickname) {
super(); super();
this.wsUri = wsUri; this.wsUri = wsUri;
this.chatID = chatID;
this.nickname = nickname;
// Create WebSocket and set internal callbacks // Create WebSocket and set internal callbacks
console.log("Initialize Client...") console.log("Initialize Client...")
@ -27,7 +29,7 @@ class Client extends EventDispatcher {
// Internal WebSocket event handlers // Internal WebSocket event handlers
_onSocketOpen(evt) { _onSocketOpen(evt) {
console.log("Connected to " + this.wsUri); console.log("Connected to " + this.wsUri);
this.sendInit(); this.sendInit(this.chatID, this.nickname);
} }
_onSocketClose(evt) { _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. * 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({ this.sendCommand({
action: "init", action: "init",
chat_id: "42", chat_id: chatID,
nickname: "binaryDiv", nickname: nickname,
}); });
} }
@ -81,6 +86,8 @@ class Client extends EventDispatcher {
/** /**
* Parses an incoming JSON message and dispatches specific events. * Parses an incoming JSON message and dispatches specific events.
*
* msgText: The content of the message as a JSON string.
*/ */
_parseMessage(msgString) { _parseMessage(msgString) {
try { try {

View File

@ -15,7 +15,9 @@ class UI {
this.initUI(); this.initUI();
// TODO start client only after the user entered their nickname // 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. * 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; const wsUri = AppSettings.serverWsUri;
this.client = new Client(wsUri); this.client = new Client(wsUri, chatID, nickname);
// Subscribe to Client events // Subscribe to Client events
this.client.on("initialized", this._onClientInit.bind(this)); this.client.on("initialized", this._onClientInit.bind(this));