improve the way to register

This commit is contained in:
Peter Maquiran
2022-01-10 09:35:17 +01:00
parent abdc9ea231
commit 053e5a2370
2 changed files with 52 additions and 33 deletions
@@ -1,11 +1,13 @@
import { Injectable } from '@angular/core';
import { v4 as uuidv4 } from 'uuid'
import { wsCallbacksParams, msgQueue } from 'src/app/models/rochet-chat-cliente-service'
class _RocketChatClientService {
connected = false
private hasPing = false
private firstPingFunx: Function
isLogin = false;
request = []
constructor() {}
@@ -25,7 +27,8 @@ class _RocketChatClientService {
this.ws.send(connectMessage)
this.ws.registerCallback('Onmessage',(message: any) => {
this.ws.registerCallback({type:'Onmessage',funx:(message: any)=>{
if(message.msg == "ping") {
this.ws.send({msg:"pong"})
if(this.hasPing == false) {
@@ -34,7 +37,8 @@ class _RocketChatClientService {
this.hasPing = true
}
}
})
}})
}
@@ -111,29 +115,30 @@ class _RocketChatClientService {
// socket class ==================================================================
private socket!: WebSocket;
private url = ''
private callBacks: {
type: 'Offline' | 'Online' | 'Open' | 'Onmessage',
object?: string
funx: Function
}[] = []
msgQueue : {
message: object,
requestId: string
}[] = []
private wsUrl = ''
wsMsgQueue : msgQueue[] = []
private wsCallbacks: {[key: string]: wsCallbacksParams} = {}
ws = {
registerCallback:(type: 'Offline' | 'Online' | 'Open' | 'Onmessage', funx: Function, object = '') =>{
this.callBacks.push({
type,
funx,
object
})
connected: false,
registerCallback:(params: wsCallbacksParams) =>{
if(!params.requestId) {
params.requestId = uuidv4()
}
this.wsCallbacks[params.requestId] = {
type: params.type,
funx: params.funx,
options: params.options || {}
}
},
deleteCallback(){},
connect:(url)=> {
this.url = url
this.socket = new WebSocket(this.url);
this.ws.connected = false
this.wsUrl = url
this.socket = new WebSocket(this.wsUrl);
// bind function
this.socket.onopen = this.ws.onopen;
this.socket.onmessage = this.ws.onmessage;
@@ -141,11 +146,10 @@ class _RocketChatClientService {
this.socket.onerror = this.ws.onerror;
},
onopen:()=> {
this.connected = true
this.ws.connected = true
console.log('================== welcome to socket server =====================')
this.msgQueue.forEach((item, index, object) => {
this.wsMsgQueue.forEach((item, index, object) => {
this.ws.send(item.message, item.requestId);
object.splice(index, 1);
})
@@ -153,11 +157,11 @@ class _RocketChatClientService {
},
send: (message: object, requestId = uuidv4()) => {
if (this.connected == false) { // save data to send when back online
if (this.ws.connected == false) { // save data to send when back online
console.log('save msgQueue')
this.msgQueue.push({message, requestId})
this.wsMsgQueue.push({message, requestId})
} else {
console.log('send rocket chat', message)
// console.log('send rocket chat', message)
let messageStr = JSON.stringify(message)
this.socket.send(messageStr)
}
@@ -168,18 +172,19 @@ class _RocketChatClientService {
const data = JSON.parse(event.data)
console.log('event.data', data)
this.callBacks.forEach((e)=>{
if(e.type== 'Onmessage') {
e.funx(data)
for (const [key, value] of Object.entries(this.wsCallbacks)) {
if(value.type== 'Onmessage') {
value.funx(data)
}
})
}
},
onclose:(event: any)=> {
this.connect(this.firstPingFunx())
this.connected = false
this.ws.connected = false
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
},