diff --git a/client/garageserver.io.js b/client/garageserver.io.js index a9887fa..f732a8f 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -20,9 +20,6 @@ window.GarageServerIO = (function (window, socketio) { registerSocketEvents = function (options) { socket.on('update', function(data) { - if(options.logging) { - console.log('garageserver.io:: socket update ' + ' ' + data.id + ' ' + data.state + ' ' + data.seq); - } updatePlayerInput(data); }); socket.on('ping', function(data) { diff --git a/example/app.js b/example/app.js index 130a494..fd1bee4 100644 --- a/example/app.js +++ b/example/app.js @@ -39,4 +39,8 @@ server.listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); }); -garageServer.createGarageServer(io.listen(server), { logging: true }); +var sockets = io.listen(server); + +sockets.set('log level', 0); + +garageServer.createGarageServer(sockets, { logging: true }); diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index 96a2284..55dd45a 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -2,14 +2,16 @@ var garageServerGame = require('./garageservergame'); function GarageServer (socketio, options) { this.io = socketio; + this.game = null; this.registerSocketEvents(options); - this.game = new garageServerGame.createGame(socketio, options); } GarageServer.prototype.registerSocketEvents = function (options) { var self = this; self.io.of('/garageserver.io').on('connection', function (socket) { + self.game = new garageServerGame.createGame(socket, options); + if(options.logging) { console.log('garageserver.io:: socket ' + socket.id + ' connection'); } diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index 806a459..e80bbd0 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -1,25 +1,26 @@ -function GarageServerGame (socketio, options) { +function GarageServerGame (socket, options) { var self = this; this.players = []; - this.io = socketio; + this.socket = socket; + this.options = options; this.physicsIntervalId = setInterval(function () { self.updatePhysics(options); }, options.physicsInterval ? options.physicsInterval : 15); this.playersIntervalId = setInterval(function () { self.updatePlayers(options); }, options.playersInterval ? options.playersInterval : 45); } -GarageServerGame.prototype.updatePlayers = function (options) { +GarageServerGame.prototype.updatePlayers = function () { var currentTime = new Date().getTime(); for(var i = 0; i < this.players.length; i ++) { - this.io.emit('update', { id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence, timestamp: currentTime }); + this.socket.emit('update', { id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence, timestamp: currentTime }); } }; -GarageServerGame.prototype.updatePhysics = function (options) { +GarageServerGame.prototype.updatePhysics = function () { for(var i = 0; i < this.players.length; i ++) { if(this.players[i].inputs.length > 0) { - if(options.onUpdatePhysics) { - this.players[i].state = options.onUpdatePhysics(this.players[i].state, this.players[i].inputs); + if(this.options.onUpdatePhysics) { + this.players[i].state = this.options.onUpdatePhysics(this.players[i].state, this.players[i].inputs); } this.players[i].inputs = []; this.players[i].sequence += this.players[i].inputs.length; @@ -33,7 +34,7 @@ GarageServerGame.prototype.addPlayer = function (client) { return; } } - + var player = { client: client, state: {},