mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 21:06:06 +00:00
75 lines
1.6 KiB
TypeScript
75 lines
1.6 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
|
export interface wss{
|
|
|
|
url: string,
|
|
type: 'reflect' | 'emit'
|
|
header: {
|
|
id: string
|
|
bluePrint: string,
|
|
jwt: string
|
|
}
|
|
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class SynchroService {
|
|
[x: string]: any;
|
|
|
|
private connection!: WebSocket;
|
|
private id: string = uuidv4();
|
|
public conected = false
|
|
private url: string = ''
|
|
|
|
constructor(){}
|
|
|
|
setUrl(wss:wss) {
|
|
this.url = `${wss.url}${wss.header.id}/${wss.header.jwt}/${wss.header.bluePrint}/${this.id}/`
|
|
}
|
|
|
|
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 = () =>{
|
|
console.log('open ======================= welcome to socket server')
|
|
// this.connection.send(JSON.stringify({user:'user1'}));
|
|
}
|
|
|
|
public $send = (object: any) => {
|
|
let sendData = JSON.stringify(Object.assign({}, object));
|
|
this.connection.send(sendData);
|
|
}
|
|
|
|
private onmessage = async (event: any)=> {
|
|
|
|
}
|
|
|
|
private onclose=(event:any)=>{
|
|
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')
|
|
this.connect()
|
|
}
|
|
}, 100);
|
|
}
|
|
|
|
private onerror=(event: any)=>{
|
|
console.log(`[error] ${event.message}`);
|
|
}
|
|
}
|