Skip to content
Permalink
Browse files

Better handle servers starting and stopping, and be able to refresh t…

…he webpage without losing the console.
  • Loading branch information...
roncli committed Jul 6, 2015
1 parent 914943b commit aea9ddef7c5fa4db00954fc0cbcbf1232b22b887
Showing with 91 additions and 13 deletions.
  1. +1 −1 package.json
  2. +12 −0 public/js/ddsn.js
  3. +41 −7 server.js
  4. +37 −5 websocket.js
@@ -18,7 +18,7 @@
"url": "https://github.com/roncli/descentDSNclient.git"
},
"dependencies": {
"descent3console": "0.1.2",
"descent3console": "0.1.4",
"descent3launcher": "0.1.3",
"descent3mn3tools": "0.1.1",
"express": "4.13.0",
@@ -476,6 +476,10 @@ var app = angular.module("ddsn", []),
var message = JSON.parse(ev.data);

switch (message.message) {
case "initservers":
data.servers = message.servers;
scope.$apply();
break;
case "missions":
data.missions = message.missions;
data.loadingMissions = false;
@@ -490,8 +494,16 @@ var app = angular.module("ddsn", []),
if (!server) {
return;
}
console.log(message.port);
if (data.currentServer.settings.server.port === message.port) {
data.currentServer = null;
if (data.serverTab === "server") {
data.serverTab = "dashboard";
}
}

data.servers.splice(data.servers.indexOf(server), 1);
server = null;

// TODO: Notification that the server was closed.

@@ -1,19 +1,23 @@
var Console = require("descent3console");
var events = require("events"),
util = require("util"),
Console = require("descent3console");

function getConsole(server, port, password) {
"use strict";

var console = new Console();
var d3console = new Console();

console.options.server = server;
console.options.port = port;
console.options.password = password;
return console;
d3console.options.server = server;
d3console.options.port = port;
d3console.options.password = password;
return d3console;
}

function Server(settings, wss) {
"use strict";

events.EventEmitter.call(this);

this.settings = settings;
this.wss = wss;

@@ -24,6 +28,8 @@ function Server(settings, wss) {
return this;
}

util.inherits(Server, events.EventEmitter);

Server.prototype.connect = function() {
"use strict";

@@ -32,10 +38,11 @@ Server.prototype.connect = function() {
this.console = getConsole(this.settings.server.ip || "localhost", this.settings.game.remoteConsolePort, this.settings.game.consolePassword);

this.console.on("connected", function() {
server.emit("connected");
server.init();
});

this.console.once("error", function(err) {
this.console.once("error", function() {
server.console = undefined;

if (new Date().getTime() > server.startTime + 60000) {
@@ -73,6 +80,8 @@ Server.prototype.init = function() {
port: server.settings.server.port,
data: line
});

server.emit("raw", line);
});

this.console.on("close", function() {
@@ -81,6 +90,10 @@ Server.prototype.init = function() {
reason: "close",
port: server.settings.server.port
});

server.console.removeAllListeners("close");
server.console.close();
server.emit("close");
});

this.console.on("end", function() {
@@ -89,6 +102,10 @@ Server.prototype.init = function() {
reason: "end",
port: server.settings.server.port
});

server.console.removeAllListeners("end");
server.console.close();
server.emit("close");
});

this.console.on("timeout", function() {
@@ -97,6 +114,23 @@ Server.prototype.init = function() {
reason: "timeout",
port: server.settings.server.port
});

server.console.removeAllListeners("timeout");
server.console.close();
server.emit("close");
});

this.console.on("error", function(err) {
server.wss.broadcast({
message: "server.close",
reason: "error",
err: err,
port: server.settings.server.port
});

server.console.removeAllListeners("error");
server.console.close();
server.emit("close");
});
};

@@ -19,7 +19,10 @@ module.exports = function() {
d.run(function() {
var settings = {},
writingSettings = false,
servers = [],
servers = {
data: [],
servers: []
},
writeSettingsCallback,

titleCase = function(text) {
@@ -51,6 +54,7 @@ module.exports = function() {
getConnectionAndGameTypes = function(d3path, callback) {
var connectionTypes = [],
gameTypes = [];

fs.readdir(path.join(d3path, "online"), function(err, connectionFiles) {
if (err) {
callback(err);
@@ -241,10 +245,17 @@ module.exports = function() {
});
}
});

// Return currently active servers.
ws.send(JSON.stringify({
message: "initservers",
servers: servers.data
}));

break;
case "launchserver":
randomPassword(function(err, password) {
var launcher;
var launcher, data, server;

if (err) {
ws.send(JSON.stringify({
@@ -261,9 +272,15 @@ module.exports = function() {
launcher.options.game.consolePassword = password;
launcher.options.game.remoteConsolePort = launcher.options.server.port;

launcher.createServer(function(err) {
var server;
data = {
settings: launcher.options,
console: [],
loading: true
};

servers.data.push(data);

launcher.createServer(function(err) {
if (err) {
ws.send(JSON.stringify({
message: "warning",
@@ -280,7 +297,22 @@ module.exports = function() {

server = new Server(launcher.options, wss);

servers.push(server);
server.on("connected", function() {
data.loading = false;
});

server.on("raw", function(line) {
data.console.push(line);
});

server.on("close", function() {
servers.data.splice(servers.data.indexOf(data), 1);
servers.servers.splice(servers.servers.indexOf(server), 1);
server = null;
data = null;
});

servers.servers.push(server);
});
});
break;

0 comments on commit aea9dde

Please sign in to comment.
You can’t perform that action at this time.