Keep web clients in sync with web socket

This commit is contained in:
Björn Dahlgren 2014-06-05 00:19:54 +02:00
parent 0612d50cd5
commit 9d55eb6162
6 changed files with 59 additions and 18 deletions

17
main.js
View File

@ -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);

View File

@ -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');
}
});
});

View File

@ -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"
}
}

View File

@ -13,6 +13,7 @@
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="/socket.io/socket.io.js"></script>
<script data-main="js/app" src="js/require.js"></script>
</head>
<body></body>

View File

@ -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 () {

View File

@ -38,6 +38,7 @@ define(function (require) {
},
serverUpdated: function() {
this.infoView.currentView.render();
this.modsView.currentView.render();
},