add synchro

This commit is contained in:
Peter Maquiran
2021-08-30 10:24:46 +01:00
parent e953831a73
commit 4309d01376
24 changed files with 338 additions and 113 deletions
+69 -13
View File
@@ -1,6 +1,8 @@
import { Injectable } from '@angular/core';
import { SessionStore } from 'src/app/store/session.service';
import { v4 as uuidv4 } from 'uuid'
import { BackgroundService } from '../background.service';
import { environment } from 'src/environments/environment';
export interface wss{
@@ -17,7 +19,7 @@ export interface wss{
@Injectable({
providedIn: 'root'
})
export class SynchroService {
class SynchroService {
[x: string]: any;
private connection!: WebSocket;
@@ -27,10 +29,12 @@ export class SynchroService {
callback = function(){}
private _connected = false;
private BackgroundService = new BackgroundService()
private callBacks: {
type: 'Offline' | 'Online'
callBacks: {
type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notification' | 'Notifications' | '',
object?: string
funx: Function
}[] = []
private msgQueue = []
constructor(){}
@@ -55,10 +59,11 @@ export class SynchroService {
this.url = `${wss.url}${wss.header.id}/${wss.header.jwt}/${wss.header.bluePrint}/${this.id}/`
}
registerCallback(type: 'Offline' | 'Online', funx: Function) {
registerCallback(type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notifications' | 'Notification', funx: Function, object='') {
this.callBacks.push({
type,
funx
funx,
object
})
}
@@ -85,22 +90,72 @@ export class SynchroService {
console.log('open ======================= welcome to socket server')
this._connected = true
// send all saved data due to internet connection
this.msgQueue.forEach((item, index, object) => {
this.$send(item);
object.splice(index, 1);
})
}
public $send(object: any) {
let message = {
message: '{"person.adress.country":"1Angola"}',
username: '',
idConnection: this.id
if(!this._connected) { // save data to send when back online
this.msgQueue.push(object)
}
let sendData = JSON.stringify(Object.assign({}, message));
let payload = {
message: JSON.stringify(object) || '{"person.adress.country":"1Angola"}',
username: SessionStore.user.FullName,
idConnection: this.id
}
let sendData = JSON.stringify(payload);
console.log(sendData)
this.connection.send(sendData);
}
private onmessage = async (event: any)=> {
let data = JSON.parse(event.data)
let payload = JSON.parse(data.message)
payload.message = JSON.parse(payload.message)
const idConnection = payload.idConnection
const username = payload.username
if(idConnection != this.id ) {
if(window['platform'].is('desktop') || this.platform.is('mobileweb')) {}
else return false
if(environment.production) return false
this.callBacks.forEach((e)=> {
if(payload.message[0]) {
if(payload.message[0].Service && payload.message[0].Object && payload.message[0].IdObject) {
if(e.type == '' && !e.object) {
if(username == SessionStore.user.FullName) {
e.funx(payload.message, data)
}
}
if(e.type == 'Notifications' ) {
e.funx(payload.message, data)
}
}
} else if(payload.message.Service && payload.message.Object && payload.message.IdObject) {
if(e.type == 'Notification' && e.object == payload.message.Object || e.type == 'Notification' && e.object == 'any' ) {
e.funx(payload.message, data)
}
}
})
}
this.callback()
}
@@ -137,7 +192,8 @@ export class SynchroService {
}
export const connection = new SynchroService()
connection.setUrl()
connection.connect()
export const synchro = new SynchroService()
synchro.setUrl()
synchro.connect()
window['synchro'] = synchro