Fixed socket connections

This commit is contained in:
Jeremiah Billmann
2013-05-21 20:07:05 -04:00
parent 864ab4eabf
commit 2f0fe7a121
4 changed files with 59 additions and 14 deletions
+1 -1
View File
@@ -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) {
+1 -1
View File
@@ -1,5 +1,5 @@
$(function () {
GarageServerIO.connectGarageServer('http://garageserver_io.jbillmann.c9.io/garageserver.io');
GarageServerIO.connectToGarageServer('http://garageserver_io.jbillmann.c9.io');
});
+33 -12
View File
@@ -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){
+24
View File
@@ -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);
};