From 2f0fe7a121be07d98277fd073517c5d452213db8 Mon Sep 17 00:00:00 2001 From: Jeremiah Billmann Date: Tue, 21 May 2013 20:07:05 -0400 Subject: [PATCH] Fixed socket connections --- client/garageserver.io.js | 2 +- example/public/javascripts/game.js | 2 +- lib/server/garageserver.io.js | 45 ++++++++++++++++++++++-------- lib/server/garageservergame.js | 24 ++++++++++++++++ 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/client/garageserver.io.js b/client/garageserver.io.js index a1dfcba..5dc7b64 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -5,7 +5,7 @@ window.GarageServerIO = (function (window, socketio) { garageServerGame = null, connectToGarageServer = function (path, options) { - io.connect(path + '/garageserver'); + io.connect(path + '/garageserver.io'); }, startGarageServerGame = function (options) { diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index b5977bb..87e489c 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -1,5 +1,5 @@ $(function () { - GarageServerIO.connectGarageServer('http://garageserver_io.jbillmann.c9.io/garageserver.io'); + GarageServerIO.connectToGarageServer('http://garageserver_io.jbillmann.c9.io'); }); \ No newline at end of file diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index a809a10..434bdfd 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -7,27 +7,48 @@ function GarageServer (socketio, options) { } GarageServer.prototype.registerSocketEvents = function (options) { - this.io.of('/garageserver.io').on('connection', function (socket) { - this.onPlayerConnect(socket, options.onConnection); - }.bind(this)); + var self = this; - this.io.of('/garageserver.io').on('disconnect', function (socket) { - this.onPlayerDisconnect(socket, options.onDisconnect); - }.bind(this)); - - this.io.of('/garageserver.io').on('input', function (socket) { - this.onPlayerInput(socket, options.onInput); - }.bind(this)); + self.io.of('/garageserver.io').on('connection', function (socket) { + console.log('garageserver.io:: socket ' + socket.id + ' connection'); + self.onPlayerConnect(socket, options); + + socket.on('disconnect', function () { + console.log('garageserver.io:: socket ' + socket.id + ' disconnect'); + self.onPlayerDisconnect(socket, options); + }); + + socket.on('input', function (input) { + self.onPlayerInput(socket, options); + }); + + socket.on('ping', function () { + self.onPlayerInput(socket, options); + }); + + }); }; GarageServer.prototype.onPlayerConnect = function (socket, callback) { - + this.game.addPlayer(socket); + if(callback.onPlayerConnect) { + callback(socket); + } }; GarageServer.prototype.onPlayerDisconnect = function (socket, callback) { + this.game.removePlayer(socket); + if(callback.onPlayerDisconnect) { + callback(socket); + } }; -GarageServer.prototype.onPlayerInput = function (socket, callback) { +GarageServer.prototype.onPlayerInput = function (socket, input, callback) { + +}; + +GarageServer.prototype.onPing = function(socket, callback) { + }; exports.createGarageServer = function (io, options){ diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index b529bbb..473481a 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -1,4 +1,5 @@ function GarageServerGame (options) { + this.players = []; this.physicsIntervalId = setInterval(this.updatePhysics, options.physicsInterval ? options.physicsInterval : 15); this.playersIntervalId = setInterval(this.updatePlayers, options.playersInterval ? options.playersInterval : 45); @@ -12,6 +13,29 @@ GarageServerGame.prototype.updatePhysics = function () { }; +GarageServerGame.prototype.addPlayer = function (client) { + for(var i = 0; i < this.players.length; i ++) { + if(this.players[i].id === client.id) { + return; + } + } + + var player = { + client: client, + inputs: [] + }; + + this.players.push(player); +}; + +GarageServerGame.prototype.removePlayer = function (client) { + for(var i = 0; i < this.players.length; i ++) { + if(this.players[i].id === client.id) { + this.players.splice(i, 1)[0]; + } + } +}; + exports.createGame = function (options){ return new GarageServerGame(options); }; \ No newline at end of file