stub socket connection to game id namespace

This commit is contained in:
Sorrel Bri 2020-01-24 15:51:15 -08:00 committed by sorrelbri
parent a4a9a72ece
commit 86a75d146e
2 changed files with 18 additions and 6 deletions

View file

@ -9,7 +9,7 @@ const show = async (req, res, next) => {
const gameId = req.params.id; const gameId = req.params.id;
if (!gameId) throw('missing game parameter') if (!gameId) throw('missing game parameter')
const game = await gameQueries.findGameById(gameId); const game = await gameQueries.findGameById(gameId);
enableGameSocket(game.room, game.id); enableGameSocket(game.room);
const record = await moveQueries.findGameRecord(gameId); const record = await moveQueries.findGameRecord(gameId);
res.status(200).json({game, record}) res.status(200).json({game, record})

View file

@ -8,7 +8,7 @@ io.on('connection', ()=> {
io.emit('connected', {message: 'socket connected'}); io.emit('connected', {message: 'socket connected'});
}) })
const enableRoomSocket = (roomId) => { const enableRoomSocket = () => {
const roomSocket = io.of(roomId); const roomSocket = io.of(roomId);
roomSocket.on('connection', (socket) => { roomSocket.on('connection', (socket) => {
@ -27,13 +27,25 @@ const enableRoomSocket = (roomId) => {
return roomSocket; return roomSocket;
} }
const enableGameSocket = (roomId, gameId) => { const enableGameSocket = (roomId) => {
const gameSocket = io.of(roomId); const gameSocket = io.of(roomId);
let game;
gameSocket.on('connection', (socket) => { gameSocket.on('connection', (socket) => {
socket.join(gameId); socket.on('joined game', (gameId) => {
socket.to(gameId).emit(`joined room ${gameId}`) game = `game-${gameId}`;
console.log(socket) console.log(gameId)
socket.join(game);
// socket
// .to(game)
io.sockets.in(game).emit('success', game)
})
}); });
// console.log(game)
// gameSocket
// .to(game)
// .emit(`success`, game)
// socket.on('hey', () => console.log('hey'))
return gameSocket; return gameSocket;