resubcribe on reconnect

This commit is contained in:
Peter Maquiran
2022-01-31 14:44:22 +01:00
parent 210a5ecbf0
commit 5bf5d15d76
3 changed files with 37 additions and 1 deletions
@@ -1,5 +1,5 @@
export interface wsCallbacksParams { export interface wsCallbacksParams {
type: 'Offline' | 'Online' | 'Open' | 'Onmessage', type: 'Offline' | 'Online' | 'Open' | 'Onmessage' | 'reConnect',
funx: Function funx: Function
runOnces?: boolean runOnces?: boolean
requestId?: string requestId?: string
@@ -54,6 +54,18 @@ export class WsChatMethodsService {
})() })()
/**
* @description when the phone is in the background for a long time it could disconnects from the socket then the socket reconnects automatically,
* when the connection is lost the subscribe is also lost, so we have to subscribe again when reconnection is establish.
*/
this.WsChatService.registerCallback({
type: 'reConnect',
funx: ()=>{
this.subscribeToRoom()
}
})
} }
openRoom(roomId) { openRoom(roomId) {
@@ -116,11 +128,17 @@ export class WsChatMethodsService {
this.loadingWholeList = false this.loadingWholeList = false
} }
/**
* @description sort room list by last message date
*/
sortRoomList() { sortRoomList() {
this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse()
this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse()
} }
/**
* @description subscribe all room
*/
subscribeToRoom() { subscribeToRoom() {
for (const id in this.dm) { for (const id in this.dm) {
+18
View File
@@ -572,6 +572,7 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
private socket!: WebSocket; private socket!: WebSocket;
private wsMsgQueue : {[key: string]: msgQueue} = {} private wsMsgQueue : {[key: string]: msgQueue} = {}
private wsCallbacks: {[key: string]: wsCallbacksParams} = {} private wsCallbacks: {[key: string]: wsCallbacksParams} = {}
private wsReconnect = 0
private ws = { private ws = {
connected: false, connected: false,
@@ -592,9 +593,26 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
}, },
onopen:()=> { onopen:()=> {
this.ws.connected = true this.ws.connected = true
console.log('================== welcome to socket server =====================') console.log('================== welcome to socket server =====================')
this.ws.wsMsgQueue() this.ws.wsMsgQueue()
if(this.wsReconnect >= 1) {
for (const [key, value] of Object.entries(this.wsCallbacks)) {
if(value.type== 'reConnect') {
const dontRepeat = value.funx()
if(dontRepeat) {
delete this.wsCallbacks[key]
}
}
}
}
this.wsReconnect++;
}, },
wsMsgQueue:()=> { wsMsgQueue:()=> {