Last active
July 10, 2020 03:04
-
-
Save benhickson/c7ff9d6fd9350d74f4c91269a0ebe4d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { Component, OnInit } from '@angular/core'; | |
import { PlayerService } from '../player.service'; | |
import io from 'socket.io-client'; | |
import { environment } from '../../../environments/environment'; | |
@Component({ | |
selector: 'app-poker', | |
templateUrl: './poker.component.html', | |
styleUrls: ['./poker.component.scss'] | |
}) | |
export class PokerComponent implements OnInit { | |
currentPlayer: number; | |
gameSocket; | |
constructor( | |
private playerService: PlayerService, | |
) { } | |
ngOnInit(): void { | |
// subscribe to data from the PlayerService | |
this.playerService.currentPlayer.subscribe(playerId => this.currentPlayer = playerId); | |
// make socket connections | |
this.gameSocket = io(environment.gameApi, { | |
transportOptions: { | |
polling: { | |
extraHeaders: { | |
'Authorization': `Bearer ${localStorage.getItem("auth_token")}` | |
} | |
} | |
} | |
}); | |
// listen for messages | |
this.gameSocket.on('game_state', this.handleGameStateMessage) | |
} | |
ngOnDestroy(): void { | |
this.playerService.changeCurrentPlayer(null); | |
} | |
// socket.io message handlers | |
handleGameStateMessage = (message): void => { | |
this.playerService.changeCurrentPlayer(message.next_player); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment