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
+16
View File
@@ -6,4 +6,20 @@ import { Injectable } from '@angular/core';
export class BackgroundService {
constructor() { }
online() {
document.body.style.setProperty(`--color`, "#0782C9");
document.body.style.setProperty(`--color2`, "#45BAFF");
document.body.style.setProperty(`--color3`, "#0782C9");
document.body.style.setProperty(`--color4`, "#0782c9f0");
document.body.style.setProperty(`--color5`, "#45BAFF");
}
offline() {
document.body.style.setProperty(`--color`, "#ffb703");
document.body.style.setProperty(`--color2`, "#ffb703");
document.body.style.setProperty(`--color3`, "#ffb703");
document.body.style.setProperty(`--color4`, "#ffb703");
document.body.style.setProperty(`--color5`, "#ffb703");
}
}
@@ -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()
}