diff --git a/main.js b/main.js index 07a3a8d..67442e5 100644 --- a/main.js +++ b/main.js @@ -1,9 +1,12 @@ -var express = require('express'), - Resource = require('express-resource'); +var express = require('express'); +var Resource = require('express-resource'); var config = require('./config'); +var manager = require('./manager'); var app = express(); +var server = require('http').Server(app); +var io = require('socket.io')(server); app.use(express.logger('dev')); app.use(express.cookieParser()); @@ -28,4 +31,12 @@ app.get('/', function (req, res){ res.sendfile(__dirname + '/public/index.html'); }); -app.listen(3000); +io.on('connection', function (socket) { + socket.emit('servers', manager.getServers()); +}); + +manager.on('servers', function() { + io.emit('servers', manager.getServers()); +}); + +server.listen(3000); diff --git a/manager.js b/manager.js index 3e5a712..d780522 100644 --- a/manager.js +++ b/manager.js @@ -1,15 +1,18 @@ -var fs = require('fs'), - spawn = require('child_process').spawn; +var events = require('events'); +var fs = require('fs'); +var spawn = require('child_process').spawn; var config = require('./config'); var filePath = "servers.json"; -function Server(id, title, port, mods) { +var Server = function (id, title, port, mods) { this.id = id; this.title = title; this.port = port; this.mods = mods; -} +}; + +Server.prototype = new events.EventEmitter(); Server.prototype.armaServerPath = function() { if (config.type === "linux") { @@ -17,15 +20,15 @@ Server.prototype.armaServerPath = function() { } return config.path + '/arma3server.exe'; -} +}; Server.prototype.makeModsParameter = function() { return '-mod=' + this.mods.join(';'); -} +}; Server.prototype.makePortParameter = function() { return '-port=' + this.port; -} +}; Server.prototype.start = function() { var startParams = []; @@ -68,15 +71,19 @@ Server.prototype.start = function() { this.pid = process.pid; this.process = process; + this.emit('started'); + return this; -} +}; Server.prototype.stop = function(cb) { var handled = false; + var self = this; this.process.on('close', function (code) { if (!handled) { handled = true; + self.emit('stopped'); cb(); } }); @@ -86,6 +93,7 @@ Server.prototype.stop = function(cb) { setTimeout(function() { if (!handled) { handled = true; + self.emit('stopped'); cb(); } }, 5000); @@ -103,12 +111,13 @@ Server.prototype.toJSON = function () { }; }; -function Manager() { +var Manager = function () { this.serversArr = []; this.serversHash = {}; - this.load(); }; +Manager.prototype = new events.EventEmitter(); + Manager.prototype.addServer = (function (id, title) { mods = []; port = 2302; @@ -118,9 +127,17 @@ Manager.prototype.addServer = (function (id, title) { }); Manager.prototype._addServer = (function (id, title, port, mods) { - var server = new Server(id, title, port, mods) + var server = new Server(id, title, port, mods); this.serversArr.push(server); this.serversHash[id] = server; + + var self = this; + var statusChanged = function () { + self.emit('servers'); + }; + server.on('started', statusChanged); + server.on('stopped', statusChanged); + return server; }); @@ -160,11 +177,17 @@ Manager.prototype.save = (function () { title: server.title, port: server.port, mods: server.mods, - }) + }); }); + var self = this; + fs.writeFile(filePath, JSON.stringify(data), function(err) { - if(err) throw err; + if (err) { + throw err; + } else { + self.emit('servers'); + } }); }); diff --git a/package.json b/package.json index bb29e59..3265ec7 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "when": "~3.1.0", "gamedig": "^0.2.4", "ip": "^0.3.0", - "playwithsix": "0.0.5" + "playwithsix": "0.0.5", + "socket.io": "^1.0.4" } } diff --git a/public/index.html b/public/index.html index a97856a..9773854 100644 --- a/public/index.html +++ b/public/index.html @@ -13,6 +13,7 @@ + diff --git a/public/js/app/router.js b/public/js/app/router.js index b0ef15b..3f84008 100644 --- a/public/js/app/router.js +++ b/public/js/app/router.js @@ -37,7 +37,11 @@ define(function (require) { layoutView.navigation.show(navigationView); missions.fetch(); mods.fetch(); - servers.fetch(); + + var socket = io.connect('http://localhost'); + socket.on('servers', function (_servers) { + servers.set(_servers); + }); }, home: function () { diff --git a/public/js/app/views/servers/view.js b/public/js/app/views/servers/view.js index d6e6c97..6337c1d 100644 --- a/public/js/app/views/servers/view.js +++ b/public/js/app/views/servers/view.js @@ -38,6 +38,7 @@ define(function (require) { }, serverUpdated: function() { + this.infoView.currentView.render(); this.modsView.currentView.render(); },