diff --git a/javascript/src/Helper/qfqChat.js b/javascript/src/Helper/qfqChat.js index 32abb060ad3aa99fb29849c8ea42c45fa47079e7..1bca72d2696daaf7c3b5b9110378a64a04b8068f 100644 --- a/javascript/src/Helper/qfqChat.js +++ b/javascript/src/Helper/qfqChat.js @@ -46,7 +46,7 @@ QfqNS.Helper = QfqNS.Helper || {}; this.topBtn = this.chatWindow.querySelector(".chat-top-symbol"); this.activateSearchBtn = this.chatWindow.querySelector(".chat-search-activate"); this.chatInput = this.chatWindow.nextElementSibling; - this.websocketUrl = this.chatWindow.getAttribute('data-websocket-url'); + this.websocketUrl = this.chatWindow.getAttribute("data-websocket-url"); this.currentSearchIndex = 0; this.searchResults = []; this.lastSearchTerm = ''; @@ -106,12 +106,14 @@ QfqNS.Helper = QfqNS.Helper || {}; }); this.form.on('form.submit.successful', (obj) => { - let elementName = this.elementName; - let newMessageId = qfqChat.getValue(obj.data, elementName); + if (this.connection) { + let elementName = this.elementName; + let newMessageId = qfqChat.getValue(obj.data, elementName); - let messageData = {value: this.chatInput.value, messageId: newMessageId}; - this.connection.send(JSON.stringify(messageData)); - console.log('send data: ' + JSON.stringify(messageData)); + let messageData = {value: this.chatInput.value, messageId: newMessageId}; + this.connection.send(JSON.stringify(messageData)); + console.log('send data: ' + JSON.stringify(messageData)); + } }); this.chatMessages.addEventListener('scroll', () => this.checkOverflow()); @@ -120,9 +122,9 @@ QfqNS.Helper = QfqNS.Helper || {}; this.scrollToBottom(); // Build up websocket connection - if (this.websocketUrl !== '') { + if (this.websocketUrl !== null && this.websocketUrl !== '') { this.connection = new WebSocket(this.websocketUrl); - this.connection.onopen = function(e) { + this.connection.onopen = (e) => { console.log("Connection established!"); let chatJsonConfigString = this.chatWindow.getAttribute('data-chat-config'); let chatJsonConfig = JSON.parse(chatJsonConfigString); @@ -136,12 +138,12 @@ QfqNS.Helper = QfqNS.Helper || {}; this.connection.send(JSON.stringify(chatConfig)); // Send heartbeat message every 30 seconds - setInterval(function() { + setInterval(() => { this.connection.send(JSON.stringify(keepConnection)); - }.bind(this), 30000); - }.bind(this); + }, 30000); + }; - this.connection.onmessage = function(e) { + this.connection.onmessage = (e) => { try { // Try to parse the data as JSON let decodedData = JSON.parse(e.data); @@ -157,13 +159,13 @@ QfqNS.Helper = QfqNS.Helper || {}; } } } catch (error) {} - }.bind(this); + }; - this.connection.onerror = function(e) { + this.connection.onerror = (e) => { console.error("Connection error!", e); }; - this.connection.onclose = function(e) { + this.connection.onclose = (e) => { console.log("Connection closed!", e); }; } else {