mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
Improve service
This commit is contained in:
@@ -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()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user