Files
doneit-web/src/app/services/socket/synchro.service.ts
T

208 lines
4.8 KiB
TypeScript
Raw Normal View History

2021-08-18 18:58:28 +01:00
import { Injectable } from '@angular/core';
2021-08-30 10:24:46 +01:00
import { SessionStore } from 'src/app/store/session.service';
2021-08-18 18:58:28 +01:00
import { v4 as uuidv4 } from 'uuid'
2021-08-27 15:30:02 +01:00
import { BackgroundService } from '../background.service';
2021-08-30 10:24:46 +01:00
import { environment } from 'src/environments/environment';
2021-08-18 18:58:28 +01:00
export interface wss{
url: string,
type: 'reflect' | 'emit'
header: {
2021-08-27 15:30:02 +01:00
id: string
bluePrint: string,
jwt: string
2021-08-18 18:58:28 +01:00
}
}
@Injectable({
providedIn: 'root'
})
2021-08-30 10:24:46 +01:00
class SynchroService {
2021-08-18 18:58:28 +01:00
[x: string]: any;
private connection!: WebSocket;
private id: string = uuidv4();
public conected = false
private url: string = ''
2021-08-19 09:10:38 +01:00
callback = function(){}
2021-08-23 14:08:58 +01:00
private _connected = false;
2021-08-27 15:30:02 +01:00
private BackgroundService = new BackgroundService()
2021-08-30 10:24:46 +01:00
callBacks: {
type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notification' | 'Notifications' | '',
object?: string
2021-08-27 15:35:29 +01:00
funx: Function
}[] = []
2021-08-30 10:24:46 +01:00
private msgQueue = []
2021-08-18 18:58:28 +01:00
constructor(){}
2021-08-23 14:08:58 +01:00
get connected() {
return this._connected
}
2021-08-19 09:10:38 +01:00
setUrl() {
let header ={
id:'1234',
bluePrint: '12312123',
jwt: uuidv4()
}
let wss: wss ={
header,
url: 'wss://synchro-server.herokuapp.com/ws/some_url/',
type: 'reflect'
}
2021-08-18 18:58:28 +01:00
this.url = `${wss.url}${wss.header.id}/${wss.header.jwt}/${wss.header.bluePrint}/${this.id}/`
}
2021-08-30 10:24:46 +01:00
registerCallback(type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notifications' | 'Notification', funx: Function, object='') {
2021-08-27 15:35:29 +01:00
this.callBacks.push({
type,
2021-08-30 10:24:46 +01:00
funx,
object
2021-08-27 15:35:29 +01:00
})
}
2021-08-18 18:58:28 +01:00
connect() {
this.connection = new WebSocket(this.url);
// bind function
this.connection.onopen = this.onopen;
this.connection.onmessage = this.onmessage;
this.connection.onclose = this.onclose;
this.connection.onerror = this.onerror;
}
private onopen = () =>{
2021-08-27 15:30:02 +01:00
2021-08-31 10:17:23 +01:00
2021-08-27 15:35:29 +01:00
2021-08-31 09:16:59 +01:00
if(!this.conected) {
2021-08-31 10:17:23 +01:00
this.BackgroundService.online()
2021-08-31 09:16:59 +01:00
this.callBacks.forEach((e)=>{
if(e.type == 'Online') {
e.funx()
}
})
}
2021-08-27 15:35:29 +01:00
2021-08-18 18:58:28 +01:00
console.log('open ======================= welcome to socket server')
2021-08-23 14:08:58 +01:00
this._connected = true
2021-08-19 09:10:38 +01:00
2021-08-30 10:24:46 +01:00
// send all saved data due to internet connection
this.msgQueue.forEach((item, index, object) => {
this.$send(item);
object.splice(index, 1);
})
2021-08-18 18:58:28 +01:00
}
2021-08-19 09:10:38 +01:00
public $send(object: any) {
2021-08-30 10:24:46 +01:00
if(!this._connected) { // save data to send when back online
this.msgQueue.push(object)
}
let payload = {
message: JSON.stringify(object) || '{"person.adress.country":"1Angola"}',
username: SessionStore.user.FullName,
2021-08-19 09:10:38 +01:00
idConnection: this.id
}
2021-08-30 10:24:46 +01:00
2021-08-19 09:10:38 +01:00
2021-08-30 10:24:46 +01:00
let sendData = JSON.stringify(payload);
console.log(sendData)
2021-08-19 09:10:38 +01:00
2021-08-18 18:58:28 +01:00
this.connection.send(sendData);
}
private onmessage = async (event: any)=> {
2021-08-30 10:24:46 +01:00
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
2021-08-31 10:43:38 +01:00
2021-08-31 11:44:10 +01:00
if(environment.production) return false
2021-08-30 10:24:46 +01:00
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)
}
}
})
}
2021-08-19 09:10:38 +01:00
this.callback()
2021-08-18 18:58:28 +01:00
}
2021-08-19 09:10:38 +01:00
private onclose=(event:any)=> {
2021-08-18 18:58:28 +01:00
setTimeout(() => {
if (event.wasClean) {
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
console.log('[close] Connection died');
console.log('Reconnect')
2021-08-27 15:35:29 +01:00
2021-08-31 09:16:59 +01:00
2021-08-27 15:35:29 +01:00
2021-08-31 09:16:59 +01:00
if(this._connected) {
this.BackgroundService.offline();
this.callBacks.forEach((e)=>{
if(e.type == 'Offline') {
e.funx()
}
})
}
2021-08-27 15:35:29 +01:00
// status
2021-08-23 14:08:58 +01:00
this._connected = false
2021-08-27 15:35:29 +01:00
// reconnect
2021-08-18 18:58:28 +01:00
this.connect()
2021-08-23 14:08:58 +01:00
2021-08-18 18:58:28 +01:00
}
}, 100);
}
private onerror=(event: any)=>{
console.log(`[error] ${event.message}`);
}
}
2021-08-19 09:10:38 +01:00
2021-08-30 10:24:46 +01:00
export const synchro = new SynchroService()
synchro.setUrl()
synchro.connect()
2021-08-25 10:40:17 +01:00
2021-08-30 10:24:46 +01:00
window['synchro'] = synchro