Skip to content

Commit

Permalink
Initial work for multiple rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenCow committed Aug 2, 2013
1 parent ccf2b69 commit 806c905
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
67 changes: 42 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,45 @@ requirejs(['./web/utils','./web/simulator','./web/wsball-game','./web/jsonwebsoc

var app = express();

var clients = [];
var newid = 1;
app.use(express.static('web'));

var simulator = new Simulator(game);
var networkServer = new NetworkServer(simulator);
var updateGame = simulator.updateGame.bind(simulator);
var insertEvent = simulator.insertEvent.bind(simulator);
var rooms = {};

function createRoom(name) {
var simulator = new Simulator(game);
var networkServer = new NetworkServer(simulator);
networkServer.messageHandlers['up'] = handleKeyMsg;
networkServer.messageHandlers['down'] = handleKeyMsg;
networkServer.messageHandlers['setname'] = handleSetname;
var room = {
simulator: simulator,
networkServer: networkServer
};
rooms[name] = room;
return room;
}

function getRoom(name) {
return rooms[name];
}

function createClientInRoom(ws,room) {
var networkServer = room.networkServer;
var messenger = new JsonWebsocketMessenger(ws);
var client = networkServer.createClient(messenger);

networkServer.clients.forEach(function(other) {
if (other === client) { return; }
if (!other.name) { return; }
client.messenger.send({
type: 'setname',
clientid: other.id,
name: other.name
});
});

networkServer.messageHandlers['up'] = handleKeyMsg;
networkServer.messageHandlers['down'] = handleKeyMsg;
networkServer.messageHandlers['setname'] = handleSetname;
return client;
}

function handleKeyMsg(msg) {
var simulator = this.server.simulator;
Expand Down Expand Up @@ -58,25 +85,15 @@ function handleSetname(msg) {
}
}

app.ws.usepath('/client',function(req,next) {
app.ws.usepath('/rooms/hallo',function(req,next) {
var roomName = 'hallo';
var room = getRoom(roomName) || createRoom(roomName);

if (!utils.contains(req.requestedProtocols,'game')) { console.log('Rejected'); return req.reject(); }
console.log('connected');
var ws = req.accept('game',req.origin);
var messenger = new JsonWebsocketMessenger(ws);
var client = networkServer.createClient(messenger);

clients.forEach(function(other) {
if (other === client) { return; }
if (!other.name) { return; }
client.messenger.send({
type: 'setname',
clientid: other.id,
name: other.name
});
});
ws.on('error',function() {
console.error('websocket error',arguments);
});
createClientInRoom(ws,room);
});


Expand Down Expand Up @@ -114,7 +131,7 @@ function update() {
});*/
}

setInterval(update,1000*(1/30));
// setInterval(update,1000*(1/30));


process.on('uncaughtException', function (err) {
Expand Down
2 changes: 1 addition & 1 deletion web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ define(['platform','game','vector','staticcollidable','linesegment','editor','re
new_uri = 'ws:';
}
new_uri += '//' + loc.host;
new_uri += '/client';
new_uri += '/rooms/hallo';

var ws = new WebSocket(new_uri, 'game');
ws.onopen = function() {
Expand Down
6 changes: 6 additions & 0 deletions web/network-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ define(['./utils'],function(utils) {
'ack': handleAck,
'resetrequest': handleResetrequest
};
this.defaultgamerate = 1000*(1/30);
this.gameupdateTimeout = setTimeout(update.bind(this), this.defaultgamerate);
}
function update() {
this.simulator.updateGame();
this.gameupdateTimeout = setTimeout(update.bind(this), this.defaultgamerate);
}
(function(p) {
p.createClient = function(messenger) {
Expand Down

0 comments on commit 806c905

Please sign in to comment.