Created
November 20, 2019 00:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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