refactor websockets for q-chat

This commit is contained in:
Phillip 2023-01-29 22:00:16 +02:00
parent 64319250a4
commit d5fee68489
3 changed files with 25 additions and 18 deletions

View File

@ -940,7 +940,6 @@ class ChatPage extends LitElement {
</div> </div>
</div> </div>
` : null} ` : null}
<div> <div>
${this.isLoadingMessages ? ${this.isLoadingMessages ?
html` html`
@ -1334,6 +1333,7 @@ class ChatPage extends LitElement {
async connectedCallback() { async connectedCallback() {
super.connectedCallback(); super.connectedCallback();
await this.initUpdate()
this.webWorker = new WebWorker(); this.webWorker = new WebWorker();
this.webWorkerImage = new WebWorkerImage(); this.webWorkerImage = new WebWorkerImage();
await this.getUpdateCompleteTextEditor(); await this.getUpdateCompleteTextEditor();
@ -1413,6 +1413,10 @@ class ChatPage extends LitElement {
disconnectedCallback() { disconnectedCallback() {
super.disconnectedCallback(); super.disconnectedCallback();
if(this.webSocket){
this.webSocket.close(1000, 'switch chat')
this.webSocket= ''
}
this.webWorker.terminate(); this.webWorker.terminate();
this.webWorkerImage.terminate(); this.webWorkerImage.terminate();
this.editor.destroy() this.editor.destroy()
@ -1561,10 +1565,6 @@ class ChatPage extends LitElement {
} }
async initUpdate(){ async initUpdate(){
if(this.webSocket){
this.webSocket.close()
this.webSocket= ''
}
this.pageNumber = 1 this.pageNumber = 1
const getAddressPublicKey = () => { const getAddressPublicKey = () => {
@ -1691,7 +1691,7 @@ class ChatPage extends LitElement {
if(isEnabledChatEnter){ if(isEnabledChatEnter){
this.isEnabledChatEnter = isEnabledChatEnter === 'false' ? false : true this.isEnabledChatEnter = isEnabledChatEnter === 'false' ? false : true
} }
await this.initUpdate()
} }
async updated(changedProperties) { async updated(changedProperties) {
@ -1703,9 +1703,6 @@ class ChatPage extends LitElement {
} }
} }
if (changedProperties && changedProperties.has('chatId') && changedProperties.get('chatId')) {
await this.initUpdate()
}
if (changedProperties && changedProperties.has('isLoading')) { if (changedProperties && changedProperties.has('isLoading')) {
@ -2213,7 +2210,7 @@ class ChatPage extends LitElement {
async fetchChatMessages(chatId) { async fetchChatMessages(chatId) {
const initDirect = async (cid) => { const initDirect = async (cid, noInitial) => {
let initial = 0 let initial = 0
let directSocketTimeout let directSocketTimeout
@ -2233,17 +2230,15 @@ class ChatPage extends LitElement {
} }
this.webSocket = new WebSocket(directSocketLink); this.webSocket = new WebSocket(directSocketLink);
// Open Connection // Open Connection
this.webSocket.onopen = () => { this.webSocket.onopen = () => {
setTimeout(pingDirectSocket, 50) setTimeout(pingDirectSocket, 50)
} }
// Message Event // Message Event
this.webSocket.onmessage = async (e) => { this.webSocket.onmessage = async (e) => {
if (initial === 0) { if (initial === 0) {
if(noInitial) return
const cachedData = null const cachedData = null
let getInitialMessages = [] let getInitialMessages = []
if (cachedData && cachedData.length !== 0) { if (cachedData && cachedData.length !== 0) {
@ -2275,8 +2270,10 @@ class ChatPage extends LitElement {
} }
// Closed Event // Closed Event
this.webSocket.onclose = () => { this.webSocket.onclose = (e) => {
clearTimeout(directSocketTimeout) clearTimeout(directSocketTimeout)
if(e.reason === 'switch chat') return
restartDirectWebSocket()
} }
// Error Event // Error Event
@ -2291,8 +2288,17 @@ class ChatPage extends LitElement {
} }
}; };
const restartDirectWebSocket = () => {
const noInitial = true
setTimeout(() => initDirect(chatId, noInitial), 50)
}
const restartGroupWebSocket = () => {
const noInitial = true
let groupChatId = Number(chatId)
setTimeout(() => initGroup(groupChatId, noInitial), 50)
}
const initGroup = (gId) => { const initGroup = (gId, noInitial) => {
let groupId = Number(gId) let groupId = Number(gId)
let initial = 0 let initial = 0
@ -2325,7 +2331,7 @@ class ChatPage extends LitElement {
this.webSocket.onmessage = async (e) => { this.webSocket.onmessage = async (e) => {
if (initial === 0) { if (initial === 0) {
if(noInitial) return
const cachedData = null; const cachedData = null;
let getInitialMessages = [] let getInitialMessages = []
if (cachedData && cachedData.length !== 0) { if (cachedData && cachedData.length !== 0) {
@ -2362,6 +2368,8 @@ class ChatPage extends LitElement {
// Closed Event // Closed Event
this.webSocket.onclose = () => { this.webSocket.onclose = () => {
clearTimeout(groupSocketTimeout) clearTimeout(groupSocketTimeout)
if(e.reason === 'switch chat') return
restartGroupWebSocket()
} }
// Error Event // Error Event

View File

@ -375,7 +375,6 @@ mwc-checkbox::shadow .mdc-checkbox::after, mwc-checkbox::shadow .mdc-checkbox::b
} }
render() { render() {
console.log('this.chatId2', this.chatId)
return html` return html`
<div <div
class=${["chatbar-container", "chatbar-buttons", this.iframeId !=="_chatEditorDOM" && 'hide-styling'].join(" ")} class=${["chatbar-container", "chatbar-buttons", this.iframeId !=="_chatEditorDOM" && 'hide-styling'].join(" ")}

View File

@ -21,7 +21,7 @@ export const replaceMessagesEdited = async ({
let responseItem = { ...response[0] } let responseItem = { ...response[0] }
const decodeResponseItem = decodeMessageFunc(responseItem, isReceipient, _publicKey) const decodeResponseItem = decodeMessageFunc(responseItem, isReceipient, _publicKey)
delete decodeResponseItem.timestamp delete decodeResponseItem.timestamp
console.log({msg})
msgItem = { msgItem = {
...msg, ...msg,
...decodeResponseItem, ...decodeResponseItem,