mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
101 lines
2.0 KiB
TypeScript
101 lines
2.0 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 = ''
|
|
callback = function(){}
|
|
|
|
constructor(){}
|
|
|
|
setUrl() {
|
|
|
|
let header ={
|
|
id:'1234',
|
|
bluePrint: '12312123',
|
|
jwt: uuidv4()
|
|
}
|
|
|
|
let wss: wss ={
|
|
header,
|
|
url: 'wss://synchro-server.herokuapp.com/ws/some_url/',
|
|
type: 'reflect'
|
|
}
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
public $send(object: any) {
|
|
|
|
let message = {
|
|
message: '{"person.adress.country":"1Angola"}',
|
|
username: '',
|
|
idConnection: this.id
|
|
}
|
|
|
|
let sendData = JSON.stringify(Object.assign({}, message));
|
|
|
|
this.connection.send(sendData);
|
|
}
|
|
|
|
private onmessage = async (event: any)=> {
|
|
this.callback()
|
|
}
|
|
|
|
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}`);
|
|
}
|
|
}
|
|
|
|
|
|
export const connection = new SynchroService()
|
|
connection.setUrl()
|
|
connection.connect() |