diff --git a/server/game-group.js b/server/game-group.js index ca64436b..5d646e42 100644 --- a/server/game-group.js +++ b/server/game-group.js @@ -181,8 +181,8 @@ GameGroup.prototype.getNumPlayers = function() { }, 0); }; -GameGroup.prototype.controllerUrl = function() { - return this.games.length > 0 ? this.games[0].controllerUrl : undefined; +GameGroup.prototype.getControllerUrl = function(baseUrl) { + return this.games.length > 0 ? this.games[0].getControllerUrl(baseUrl) : undefined; }; GameGroup.prototype.sendQuit = function() { diff --git a/server/game.js b/server/game.js index cff12e36..538a6c4f 100644 --- a/server/game.js +++ b/server/game.js @@ -36,7 +36,6 @@ var hftSite = require('./hftsite'); /** * @typedef {object} Game~Options - * @property {string} baseUrl the base url used to make urls */ /** @@ -57,13 +56,14 @@ var Game = function(id, gameGroup, options) { this.numPlayers = 0; this.sendQueue = []; this.options = options; - if (this.runtimeInfo) { - this.controllerUrl = options.baseUrl + "/games/" + this.runtimeInfo.info.happyFunTimes.gameId + "/index.html"; - } this.setGameId(); debug("create game " + this. gameId); }; +Game.prototype.getControllerUrl = function(baseUrl) { + return this.runtimeInfo ? baseUrl + "/games/" + this.runtimeInfo.info.happyFunTimes.gameId + "/index.html" : undefined; +}; + Game.prototype.setGameId = function() { this.gameId = (this.runtimeInfo ? this.runtimeInfo.info.happyFunTimes.gameId : "") + " id=" + this.id; }; diff --git a/server/hft-server.js b/server/hft-server.js index fd3ebd3f..d72c4d16 100644 --- a/server/hft-server.js +++ b/server/hft-server.js @@ -47,6 +47,7 @@ var HFTGame = require('./hftgame'); var hftSite = require('./hftsite'); var highResClock = require('../lib/highresclock'); var http = require('http'); +var iputils = require('../lib/iputils'); var log = require('../lib/log'); var mime = require('mime'); var NonRequire = require('./non-require'); @@ -99,6 +100,34 @@ var HFTServer = function(options, startedCallback) { g[prop] = options[prop]; }); + var updateIpAddress = function(address) { + if (relayServer) { + relayServer.setOptions({baseUrl: getBaseUrl()}); + } + hftSite.setup({address: address}); + if (appleCaptivePortalHandler) { + appleCaptivePortalHandler.setOptions({address: address}); + } + }; + + var getAddress = (function() { + var oldAddress; + + return function() { + var address = g.address || iputils.getOneIpAddress(); + if (address !== oldAddress) { + oldAddress = address; + console.log("using ip address: " + address); + updateIpAddress(address); + } + return address; + }; + }()); + + var getBaseUrl = function() { + return "http://" + getAddress() + ":" + g.port; + }; + g.gameDB = g.gameDB || new AvailableGames(); var eventEmitter = new events.EventEmitter(); @@ -111,7 +140,11 @@ var HFTServer = function(options, startedCallback) { var app = express(); var relayServer; - hftSite.setup(g); + hftSite.setup({ + address: getAddress(), + port: g.port, + privateServer: g.privateServer, + }); g.cwd = path.normalize(path.join(__dirname, "..")); @@ -308,7 +341,7 @@ var HFTServer = function(options, startedCallback) { var appleCaptivePortalHandler = new AppleCaptivePortalHandler({ baseDir: path.join(g.cwd, g.baseDir), - address: g.address, + address: getAddress(), port: g.port, sendFileFn: sendFileResponse, }); @@ -377,7 +410,7 @@ var HFTServer = function(options, startedCallback) { } var prepFn = isTemplate ? (function() { var params = [{ - localhost: g.address, + localhost: getAddress(), }]; if (runtimeInfo) { params.push(runtimeInfo); @@ -521,8 +554,7 @@ var HFTServer = function(options, startedCallback) { } var RelayServer = require('./relayserver.js'); relayServer = options.relayServer || new RelayServer(servers, { - address: g.address, - baseUrl: "http://" + g.address + ":" + g.port, + baseUrl: getBaseUrl(), noMessageTimout: options.inactivityTimeout, gameDB: g.gameDB, }); diff --git a/server/relayserver.js b/server/relayserver.js index 7c1d10f0..0a15142e 100644 --- a/server/relayserver.js +++ b/server/relayserver.js @@ -45,9 +45,6 @@ var WSServer = require('./websocketserver'); * RelayServer options * @typedef {Object} RelayServer~Options * @property {AvailableGames} gameDB The game db - * @property {String} address - address that will - * replace "localhost" when a game's controllerUrl is - * passed in. * @property {String?} baseUrl ??? * @property {WebSocketServer?} WebSocketServer constructor for WebSocketServer (for testing) */ @@ -79,10 +76,22 @@ var RelayServer = function(servers, options) { var g_nextSessionId = 0; var g_gameGroups = {}; var g_numGameGroups = 0; + var g_options = {}; var socketServers = []; var eventEmitter = new events.EventEmitter(); var gameDB = options.gameDB; + this.setOptions = function(options) { + ["baseUrl"].forEach(function(key) { + var value = options[key]; + if (value !== undefined) { + g_options[key] = value; + } + }); + }; + + this.setOptions(options); + this.on = eventEmitter.on.bind(eventEmitter); this.addListener = this.on; this.removeListener = eventEmitter.removeListener.bind(eventEmitter); @@ -130,7 +139,7 @@ var RelayServer = function(servers, options) { } var gameGroup = g_gameGroups[gameId]; if (!gameGroup && makeGroup) { - gameGroup = new GameGroup(gameId, this, { gameDB: gameDB, baseUrl: options.baseUrl }); + gameGroup = new GameGroup(gameId, this, { gameDB: gameDB }); g_gameGroups[gameId] = gameGroup; ++g_numGameGroups; debug("added game group: " + gameId + ", num game groups = " + g_numGameGroups); @@ -161,7 +170,7 @@ var RelayServer = function(servers, options) { gameId: id, serverName: computerName.get().trim(), numPlayers: gameGroup.getNumPlayers(), - controllerUrl: gameGroup.controllerUrl(), + controllerUrl: gameGroup.getControllerUrl(g_options.baseUrl), runtimeInfo: gameGroup.runtimeInfo, }); }