import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import * as io from 'socket.io-client'

@Injectable({
  providedIn: 'root'
})
export class WebSocketService{

  socket: any;

 /*  readonly uri: string = "https://www.ferrariventilatori.mx/web-socket"; 
  constructor() { 
    this.socket = io.io(this.uri, { path: "/web-socket" });
  }
 */
  // PRUEBAS EN LOCAL
  readonly uri: string = "ws://localhost:4444"; 
  constructor() { 
   /*  this.socket = io.io(this.uri, { transports: ['websocket']}); */    
   if (window.location.href.includes("localhost")) {
    this.socket = io.io("ws://localhost:4444", { transports: ['websocket']});
   }else{
    this.socket = io.io();
   }
    
  }  
 
  listen(eventName: string){
    return new Observable((subscribe) => {
      this.socket.on(eventName, (data:any) => {
        subscribe.next(data);
      })
    })
  }

  emit(eventName: string, data:any){
    this.socket.emit(eventName, data);
  }
}
