Skip to content
Snippets Groups Projects
Commit 60f4448c authored by enured's avatar enured
Browse files

F17462: Websocket implementation V1. refs #17462

parent 681698d2
No related branches found
No related tags found
2 merge requests!691New version v24.3.0,!667F17540: FE Chat v1.2
Pipeline #11756 passed
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment