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-09-21 06:09:41 +01:00
|
|
|
import { EventTrigger } from '../eventTrigger.service'
|
2021-08-18 18:58:28 +01:00
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
export interface wss {
|
2021-08-18 18:58:28 +01:00
|
|
|
|
|
|
|
|
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-10-07 16:22:31 +01:00
|
|
|
[x: string]: any;
|
2021-08-18 18:58:28 +01:00
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
private connection!: WebSocket;
|
2021-08-18 18:58:28 +01:00
|
|
|
private id: string = uuidv4();
|
|
|
|
|
public conected = false
|
|
|
|
|
private url: string = ''
|
2021-10-07 16:22:31 +01:00
|
|
|
callback = function () { }
|
2021-08-23 14:08:58 +01:00
|
|
|
private _connected = false;
|
2021-09-21 06:09:41 +01:00
|
|
|
private eventtrigger: EventTrigger;
|
2021-08-27 15:30:02 +01:00
|
|
|
private BackgroundService = new BackgroundService()
|
2021-08-30 10:24:46 +01:00
|
|
|
callBacks: {
|
2021-10-07 16:22:31 +01:00
|
|
|
type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notification' | 'Notifications' | '',
|
2021-08-30 10:24:46 +01:00
|
|
|
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
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
constructor() {
|
2021-09-27 16:23:41 +01:00
|
|
|
// alert(SessionStore.user.FullName)
|
|
|
|
|
}
|
2021-08-18 18:58:28 +01:00
|
|
|
|
2021-08-23 14:08:58 +01:00
|
|
|
get connected() {
|
|
|
|
|
return this._connected
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 09:10:38 +01:00
|
|
|
setUrl() {
|
|
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
let header = {
|
|
|
|
|
id: '1234',
|
2021-08-19 09:10:38 +01:00
|
|
|
bluePrint: '12312123',
|
|
|
|
|
jwt: uuidv4()
|
|
|
|
|
}
|
2021-09-30 10:21:58 +01:00
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
let wss: wss = {
|
2021-08-19 09:10:38 +01:00
|
|
|
header,
|
2021-09-30 10:21:58 +01:00
|
|
|
url: 'wss://sychro-offline.herokuapp.com/ws/some_url/',
|
2021-08-19 09:10:38 +01:00
|
|
|
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-10-07 16:22:31 +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-10-07 16:22:31 +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 = () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//if (this._connected === true) {
|
2021-10-18 17:42:25 +01:00
|
|
|
//this.BackgroundService.online()
|
2021-10-07 16:22:31 +01:00
|
|
|
console.log('Online', this._connected)
|
|
|
|
|
this.callBacks.forEach((e) => {
|
|
|
|
|
if (e.type == 'Online') {
|
2021-08-31 09:16:59 +01:00
|
|
|
e.funx()
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-10-07 16:22:31 +01:00
|
|
|
//}
|
2021-08-27 15:35:29 +01:00
|
|
|
|
2021-08-18 18:58:28 +01:00
|
|
|
console.log('open ======================= welcome to socket server')
|
2021-10-07 16:22:31 +01:00
|
|
|
|
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-10-07 16:22:31 +01:00
|
|
|
}
|
2021-08-18 18:58:28 +01:00
|
|
|
|
2021-08-19 09:10:38 +01:00
|
|
|
public $send(object: any) {
|
|
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
if (!this._connected) { // save data to send when back online
|
2021-08-30 10:24:46 +01:00
|
|
|
this.msgQueue.push(object)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
let payload = {
|
2021-08-30 10:24:46 +01:00
|
|
|
message: JSON.stringify(object) || '{"person.adress.country":"1Angola"}',
|
|
|
|
|
username: SessionStore.user.FullName,
|
2021-08-19 09:10:38 +01:00
|
|
|
idConnection: this.id
|
|
|
|
|
}
|
2021-10-07 16:22:31 +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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
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
|
|
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
if (idConnection != this.id) {
|
|
|
|
|
|
|
|
|
|
if (window['platform'].is('desktop') || this.platform.is('mobileweb')) { }
|
2021-08-30 10:24:46 +01:00
|
|
|
else return false
|
2021-08-31 10:43:38 +01:00
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
if (environment.production) return false
|
2021-08-30 10:24:46 +01:00
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
this.callBacks.forEach((e) => {
|
2021-08-30 10:24:46 +01:00
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
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) {
|
2021-08-30 10:24:46 +01:00
|
|
|
e.funx(payload.message, data)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-07 16:22:31 +01:00
|
|
|
|
|
|
|
|
if (e.type == 'Notifications') {
|
2021-08-30 10:24:46 +01:00
|
|
|
e.funx(payload.message, data)
|
|
|
|
|
}
|
2021-10-07 16:22:31 +01:00
|
|
|
|
2021-08-30 10:24:46 +01:00
|
|
|
}
|
2021-10-07 16:22:31 +01:00
|
|
|
} 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') {
|
2021-08-30 10:24:46 +01:00
|
|
|
e.funx(payload.message, data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
2021-08-19 09:10:38 +01:00
|
|
|
this.callback()
|
2021-10-07 16:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onclose = (event: any) => {
|
|
|
|
|
console.log('Websocket close')
|
|
|
|
|
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');
|
2021-08-18 18:58:28 +01:00
|
|
|
console.log('Reconnect')
|
2021-08-27 15:35:29 +01:00
|
|
|
|
2021-10-07 16:22:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (this._connected === false) {
|
2021-10-18 17:42:25 +01:00
|
|
|
// this.BackgroundService.offline();
|
2021-10-07 16:22:31 +01:00
|
|
|
console.log('Offline', this._connected)
|
|
|
|
|
this.callBacks.forEach((e) => {
|
|
|
|
|
if (e.type == 'Offline') {
|
2021-08-31 09:16:59 +01:00
|
|
|
e.funx()
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-10-07 16:22:31 +01:00
|
|
|
//}
|
|
|
|
|
|
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-10-07 16:22:31 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}, 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onerror = (event: any) => {
|
|
|
|
|
console.log(`[error] ${event.message}`);
|
|
|
|
|
}
|
2021-08-18 18:58:28 +01:00
|
|
|
}
|
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
|