Skip to content

Commit

Permalink
commented consoles
Browse files Browse the repository at this point in the history
fixed socket room leak
  • Loading branch information
rodikh committed Jan 14, 2014
1 parent f256c83 commit 8b03c8b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TODO:

Known Issues:

* socket events sent to all rooms instead of only socket's room
* ~~socket events sent to all rooms instead of only socket's room~~

Future:

Expand Down
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var server = http.createServer(app).listen(app.get('port'), function () {
console.log('Server is listening on port:', app.get('port'));
});

var io = socketio.listen(server, { log: false });
var io = socketio.listen(server, { log: true });

// Configure routes
require('./routes')(app);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/gameController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
};
this.makeMove = function (x, player) {
if (parseInt(player, 10) === turn) {
console.log("making move", x, player);
// console.log("making move", x, player);
board[x] = player;
turn = 3 - turn;
return board;
Expand Down
21 changes: 11 additions & 10 deletions src/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ module.exports = function (app, io) {

io.sockets.on('connection', function (socket) {
socket.on('joinGame', function (gameId, callback) {
socket.join(gameId);
socket.join('game'+gameId);
var game = lobby.getGame(gameId);
var player = game.addPlayer(socket);
if (player) {
socket.game = game;
socket.playerId = player;
console.log("player:" + player + " joined game:" + gameId);
io.sockets.in(gameId).emit('playerJoined', socket.playerId);
// console.log("player:" + player + " joined game:" + gameId);
io.sockets.in('game'+gameId).emit('playerJoined', socket.playerId);
if (typeof callback === "function") {
callback({playerId: player, players: game.getPlayers()});
}
} else{
console.log("game " + gameId + " is full! cannot join");
// console.log("game " + gameId + " is full! cannot join");
}
});
socket.on('disconnect', function () {
if (socket.game) {
var index = socket.game.removePlayer(socket);
io.sockets.in(socket.game.id).emit('playerLeft', socket.playerId);
io.sockets.in('game'+socket.game.id).emit('playerLeft', socket.playerId);
}
});
socket.on('getBoard', function (callback) {
Expand All @@ -39,14 +39,15 @@ module.exports = function (app, io) {
if (game){
var board = game.makeMove(data.x, socket.playerId);
callback(board);
io.sockets.in(game.id).emit('boardChanged', board);
io.sockets.in('game'+game.id).emit('boardChanged', board);
console.log("emmiting to ", game.id, " sockets: ", io.sockets.in('game'+game.id));

var checkWin = game.checkWin();
console.log("checkWin", checkWin);
// console.log("checkWin", checkWin);
if (checkWin !== 0) {
io.sockets.in(game.id).emit('gameOver', checkWin);
io.sockets.in('game'+game.id).emit('gameOver', checkWin);
var newBoard = game.resetBoard();
io.sockets.in(game.id).emit('boardChanged', newBoard);
io.sockets.in('game'+game.id).emit('boardChanged', newBoard);
}
}
}
Expand All @@ -57,7 +58,7 @@ module.exports = function (app, io) {
var game = lobby.getGame(socket.game.id);
if (game){
var newBoard = game.resetBoard();
io.sockets.in(game.id).emit('boardChanged', newBoard);
io.sockets.in('game'+game.id).emit('boardChanged', newBoard);
}
}
});
Expand Down

0 comments on commit 8b03c8b

Please sign in to comment.