Browse files

fixing space game to work multiplayer again

  • Loading branch information...
1 parent be8d91f commit 9133ac185550aa6c2d409131701aa92091be31a5 brucero committed Oct 17, 2011
Showing with 52 additions and 42 deletions.
  1. +4 −17 engine/core/xhr.js
  2. +31 −16 lib/socket.js
  3. +1 −1 lib/svrcmds.js
  4. +1 −1 pvn/publish.js
  5. +5 −2 space/game-multi.shtml
  6. +10 −5 space/game.js
View
21 engine/core/xhr.js
@@ -16,25 +16,11 @@ var Xhr = (function() {
var socket;
function init() {
- if (typeof(stand_alone) != 'undefined') {
+ if (stand_alone) {
return;
}
var options = {transports: ['xhr-polling']};
- switch (Browser.browser) {
- case Browser.CHROME:
- case Browser.WEBKIT:
- options.transports = ['websocket', 'xhr-polling'];
- break;
- }
- var transports = {'websocket':1, 'xhr-polling':1, 'flashsocket':1};
- var list = document.URL.split('/');
- for(var i in list) {
- if (transports[list[i]]) {
- options.transports = list[i];
- }
- }
- socket = new io.Socket(null, options);
- socket.connect();
+ socket = io.connect("http://localhost:8081", options);
socket.on('message', function(data) {
data = JSON.parse(data.replace('<', '&lt;').replace('>', '&gt;'));
@@ -45,9 +31,10 @@ var Xhr = (function() {
}
function toServer(cmd) {
- if (typeof(stand_alone) != 'undefined') {
+ if (stand_alone) {
return;
}
+ console.log('sent cmd: '+JSON.stringify(cmd));
var unique_id = client_user.unique_id;
var script = document.createElement('script');
var req_id = 'id' + unique_id + (new Date).getTime();
View
47 lib/socket.js
@@ -19,6 +19,7 @@ function sendMessage(client, id, cmds) {
}
function handleDataReq(client, message) {
+ console.log('got msg: '+message);
var user = Users.findSession(message.user_id);
if (!user) {
@@ -39,24 +40,38 @@ function handleDataReq(client, message) {
Server.processUserCmds(user, message.req_id, message.cmd);
}
+function init2(server) {
+ var conn = io.listen(server);
+ conn.sockets.on('connection', function (socket) {
+ socket.emit('news', { hello: 'world' });
+ socket.on('message', function (data) {
+ console.log('\n\nmesaage:\n'+data);
+ });
+ });
+}
+
function init(server) {
- socket = io.listen(server);
- socket.on('connection', function(client) {
- client.on('message', function(message) {
- try {
- var message = JSON.parse(message.replace('<', '&lt;').replace('>', '&gt;'));
- } catch (SyntaxError) {
- log('Invalid JSON:');
- log(message);
- return false;
- }
- handleDataReq(client, message);
- });
-
- client.on('disconnect', function() {
- Users.removeSession(client.user_id);
- });
+ console.log('\nlisten init\n');
+ var conn = io.listen(server);
+ conn.sockets.on('connection', function(client) {
+ console.log('connect');
+ client.on('message', function(message) {
+ console.log('message '+message);
+ try {
+ var message = JSON.parse(message.replace('<', '&lt;').replace('>', '&gt;'));
+ } catch (SyntaxError) {
+ log('Invalid JSON:');
+ log(message);
+ return false;
+ }
+ handleDataReq(client, message);
+ });
+
+ client.on('disconnect', function() {
+ console.log('disconnect');
+ Users.removeSession(client.user_id);
});
+ });
}
exports.init = init;
View
2 lib/svrcmds.js
@@ -69,7 +69,7 @@ install('ping', ping);
install('echo', echo);
exports.exec = function(user, command) {
- //console.log('cmd: ' + JSON.stringify(command));
+ console.log('cmd: ' + JSON.stringify(command));
var ret = Utils.cmd_exec(server_cmds, user, command);
if (ret)
return ret;
View
2 pvn/publish.js
@@ -203,7 +203,7 @@ var Publish = (function() {
function checkReplayUrl() {
var pathname = window.location.search,
- prefix = '?show=',
+ prefix = '?show=';
i = pathname.indexOf(prefix);
if (i < 0) {
return;
View
7 space/game-multi.shtml
@@ -5,8 +5,11 @@
<script type="text/javascript">var stand_alone=false;</script>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
#include '/engine/include/core.include'
+
+ <script type="text/javascript" src="/engine/core/xhr.js"> </script>
+ <script type="text/javascript" src="/perf/benchmark.js"> </script>
+ <script type="text/javascript" src="/perf/perftest.js"> </script>
<script type="text/javascript" src="/space/game.js"> </script>
- <script type="text/javascript" src="/space/svrgame.js"> </script>
</head>
<body id="body_id" style="overflow:hidden;"
@@ -15,7 +18,7 @@
onorientationchange="Init.winresize();"
onunload="Init.quit()">
<div id="fb-root"></div>
- <div id="gamebody"></div>
+ <div id="gamebody" tabindex="0"></div>
</body>
</html>
View
15 space/game.js
@@ -155,7 +155,7 @@ var Game = (function() {
dir[0] = -Math.sin(me.angle);
dir[1] = -Math.cos(me.angle);
dir = Vec.norm(dir);
- makeMob(me.extent[0], dir, 'shot');
+ makeMob(me.extent[0], dir, 'shot');
}
function checkStanding() {
@@ -540,12 +540,17 @@ var Game = (function() {
client_user.grids[1].removeCB = Gob.del;
}
- function initStandalone() {
+ function initGame() {
Input.hookEvents('gamebody');
- SvrGame.init(client_user.grids);
+ if (stand_alone) {
+ SvrGame.init(client_user.grids);
+ }
}
function playGame() {
+ if (!stand_alone) {
+ Xhr.init();
+ }
UI.del('buttons');
UI.del('perf');
if (window.location.pathname.match(/canvas/)) {
@@ -561,9 +566,9 @@ var Game = (function() {
Game.fbLogin = fbLogin;
Game.tick = tick;
Game.init = init;
- Game.initStandalone = initStandalone;
Game.playGame = playGame;
+ Game.initGame = initGame;
return Game;
})();
-Init.setFunctions({app: Game.tick, draw: Render.tick, ui: UI.tick, setup: Game.playGame, init: Game.initStandalone, fps:60});
+Init.setFunctions({app: Game.tick, draw: Render.tick, ui: UI.tick, setup: Game.playGame, init: Game.initGame, fps:60});

0 comments on commit 9133ac1

Please sign in to comment.