node-go/packages/play-node-go/server/socket.js
2020-02-05 16:44:37 -08:00

53 lines
1.4 KiB
JavaScript

// TODO const someSocketLogic = require('./middleware/socketssockets/...');
const socketIO = require('socket.io');
const io = socketIO({ cookie: false });
const gameQueries = require('./data/queries/game');
const gameServices = require('./services/gameServices');
io.on('connection', socket=> {
socket.emit('connected', {message: 'socket connected'});
socket.on('connect_room', data => {
console.log(data)
if (data.user && data.user.email) {
delete data.user.email;
}
const room= data.room;
const roomIo = io.of(room);
roomIo.on('connection', socket => {
socket.emit('connected')
socket.emit('new_user', data);
socket.on('connect_game', data => {
const game = `game-${data.game.id}`;
socket.join(game, () => {
io.of(room).to(game).emit('game_connected', {})
});
});
socket.on('make_move', data => {
const { user, move, board, game, room } = data;
gameServices.placeMove(1, {player: 'black', move: '7,4'})
console.log(data)
})
});
})
})
const roomSocket = (roomId) => {
const roomIo = io.of(roomId)
roomIo.on('connection', socket => {
console.log('connected room')
socket.on('connect_room', data => {
if (data.user && data.user.email) {
delete data.user.email;
}
socket.emit('new_user', data);
})
})
return roomIo;
}
module.exports = {
io,
roomSocket
}