import { Injectable } from "@angular/core" ; import * as scoket from "socket.io-client" ; import { environment } from "../../environments/environment" ; @ Injectable ( ) export class SocketService { private url : string = environment . apiUrl ; private socket : SocketIOClient . Socket ; constructor ( ) { this . init ( ) ; } init ( ) { let socketUrl = new URL ( this . url ) ; let socketPath = "socket" ; let path ; if ( socketUrl . pathname . endsWith ( "/" ) ) path = `${ socketUrl . pathname }${ socketPath }` ; else path = `${ socketUrl . pathname }/${ socketPath }` ; this . socket = scoket . connect ( socketUrl . origin , { path : path } ) ; this . socket . on ( "connect" , ( ) => console . log ( "socket connected" ) ) ; this . socket . on ( "disconnect" , ( ) => console . log ( "socket disconnected" ) ) ; this . socket . on ( "error" , ( error ) => console . error ( `scoket error: "${ error }"` ) ) ; } getSystemFailureTopic ( ) { return "data_failure" ; } getSystemStatsTopic ( ) { return "data_stats" ; } subscribe ( topic , handler ) { if ( topic === null || topic === undefined ) return ; this . socket . emit ( "sub" , topic ) ; this . socket . on ( topic , handler ) ; console . log ( `subscribed to ${ topic }` ) ; } unsubscribe ( topic ) { if ( topic === null || topic === undefined ) return ; this . socket . emit ( "unsub" , topic ) ; console . log ( `unsubscribed from ${ topic }` ) ; } }