Improve service

This commit is contained in:
Peter Maquiran
2021-08-27 15:35:29 +01:00
parent 3367d8cef0
commit 4f8c547672
2 changed files with 44 additions and 0 deletions
@@ -27,6 +27,10 @@ export class SynchroService {
callback = function(){}
private _connected = false;
private BackgroundService = new BackgroundService()
private callBacks: {
type: 'Offline' | 'Online'
funx: Function
}[] = []
constructor(){}
@@ -51,6 +55,13 @@ export class SynchroService {
this.url = `${wss.url}${wss.header.id}/${wss.header.jwt}/${wss.header.bluePrint}/${this.id}/`
}
registerCallback(type: 'Offline' | 'Online', funx: Function) {
this.callBacks.push({
type,
funx
})
}
connect() {
this.connection = new WebSocket(this.url);
@@ -64,6 +75,13 @@ export class SynchroService {
private onopen = () =>{
this.BackgroundService.online()
this.callBacks.forEach((e)=>{
if(e.type == 'Online') {
e.funx()
}
})
console.log('open ======================= welcome to socket server')
this._connected = true
@@ -95,8 +113,18 @@ export class SynchroService {
// event.code is usually 1006 in this case
console.log('[close] Connection died');
console.log('Reconnect')
this.BackgroundService.offline();
this.callBacks.forEach((e)=>{
if(e.type == 'Offline') {
e.funx()
}
})
// status
this._connected = false
// reconnect
this.connect()
}