Skip to content

Instantly share code, notes, and snippets.

@Klerith
Created November 20, 2019 00:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Klerith/22be96868c537be7b4fed322c1d35b9e to your computer and use it in GitHub Desktop.
Save Klerith/22be96868c537be7b4fed322c1d35b9e to your computer and use it in GitHub Desktop.
Servicio de Angular para escuchar y emitir eventos mediante sockets de socket.io
import { Injectable } from '@angular/core';
import { Socket } from 'ngx-socket-io';
@Injectable({
providedIn: 'root'
})
export class WebsocketService {
public socketStatus = false;
public usuario = null;
constructor(
private socket: Socket
) {
this.checkStatus();
}
checkStatus() {
this.socket.on('connect', () => {
console.log('Conectado al servidor');
this.socketStatus = true;
});
this.socket.on('disconnect', () => {
console.log('Desconectado del servidor');
this.socketStatus = false;
});
}
emit( evento: string, payload?: any, callback?: Function ) {
console.log('Emitiendo', evento);
// emit('EVENTO', payload, callback?)
this.socket.emit( evento, payload, callback );
}
listen( evento: string ) {
return this.socket.fromEvent( evento );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment