Merge pull request #21 from alexfriesen/feature-disconnect

Feature: disconnect
This commit is contained in:
Garry Newman
2017-04-22 10:20:17 +01:00
committed by GitHub
7 changed files with 376 additions and 241 deletions
+59
View File
@@ -1,2 +1,61 @@
.DS_Store
Thumbs.db
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
+52 -13
View File
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en" ng-app="RconApp">
<head>
<meta charset="utf-8">
@@ -12,34 +12,72 @@
</head>
<body ng-controller="RconController">
<nav class="navbar navbar-default" ng-controller="ServerInfoController" ng-show="info.Hostname != null">
<nav class="navbar navbar-default" ng-controller="ServerInfoController" ng-show="Connected">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">{{info.Hostname}}</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse-header" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">{{info.Hostname}}</a>
</div>
<div class="collapse navbar-collapse" id="navbar-collapse-header">
<ul class="nav navbar-nav">
<li><div class="navbar-info" title="Uptime"><span class="glyphicon glyphicon-time"></span> {{info.Uptime|SecondsToDuration}}</div></li>
<li><div class="navbar-info" title="Map Name"><span class="glyphicon glyphicon-globe"></span> {{info.Map}}</div></li>
<li><div class="navbar-info" title="Current Players/Maximum Players"><span class="glyphicon glyphicon-user"></span> {{info.Players|number}}/{{info.MaxPlayers|number}}</div></li>
<li><div class="navbar-info" title="Framerate"><span class="glyphicon glyphicon-hourglass"></span> {{info.Framerate|number}}fps</div></li>
<li><div class="navbar-info" title="Entity Count"><span class="glyphicon glyphicon-oil"></span> {{info.EntityCount|number}}</div></li>
<li>
<p class="navbar-text" title="Uptime">
<span class="glyphicon glyphicon-time"></span>
{{info.Uptime|SecondsToDuration}}
</p>
</li>
<li>
<p class="navbar-text" title="Map Name">
<span class="glyphicon glyphicon-globe"></span>
{{info.Map}}
</p>
</li>
<li>
<p class="navbar-text" title="Current Players/Maximum Players">
<span class="glyphicon glyphicon-user"></span>
{{info.Players|number}}/{{info.MaxPlayers|number}}
</p>
</li>
<li>
<p class="navbar-text" title="Framerate">
<span class="glyphicon glyphicon-hourglass"></span>
{{info.Framerate|number}}fps
</p>
</li>
<li>
<p class="navbar-text" title="Entity Count">
<span class="glyphicon glyphicon-oil"></span>
{{info.EntityCount|number}}
</p>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<button class="btn btn-danger navbar-btn" type="button" ng-click="Disconnect()">Disconnect</button>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div ng-include="'html/connect.html'" ng-show="!Connected">
</div>
<div ng-include="'html/connect.html'" ng-show="!Connected"></div>
<div ng-show="Connected" class="col-sm-2 sidebar">
<ul class="nav nav-sidebar">
<li ng-repeat="p in pages | filter: { Nav: '' } " ng-class="{'active' : $route.current.Title == p.Title }">
<a href="#{{Nav( p.originalPath )}}">{{p.Title}}</a>
<li ng-repeat="page in pages" ng-class="{'active' : $route.current.Title == page.Title }">
<a href="#{{Nav( page.originalPath )}}">{{page.Title}}</a>
</li>
</ul>
@@ -49,6 +87,7 @@
<h1 class="page-header">{{$route.current.Title}}</h1>
<div class="row" ng-view></div>
</div>
</div>
+73 -60
View File
@@ -1,97 +1,110 @@
var app = angular.module('RconApp', ['ngRoute', 'nvd3']);
app.service('rconService', [RconService]);
var app = angular.module( 'RconApp', ['ngRoute', 'nvd3'] );
app.config(function($routeProvider) {
$routeProvider.when("/home", {Title: "Home"});
$routeProvider.when("/:address/info", {
Title: "Server",
templateUrl: "html/serverInfo.html",
Nav: true
});
$routeProvider.when("/:address/console", {
Title: "Console",
templateUrl: "html/console.html",
Nav: true
});
$routeProvider.when("/:address/chat", {
Title: "Chat",
templateUrl: "html/chat.html",
Nav: true
});
$routeProvider.when("/:address/playerlist", {
Title: "Player List",
templateUrl: "html/playerlist.html",
Nav: true
});
$routeProvider.when("/:address/player/:userid", {
Title: "Player Info",
templateUrl: "html/playerInfo.html"
});
$routeProvider.otherwise({redirectTo: '/home'});
});
app.service( 'rconService', [RconService] );
app.controller('RconController', RconController);
app.config( function ( $routeProvider )
{
$routeProvider.when( "/home", { Title: "Home" } )
$routeProvider.when( "/:address/info", { Title: "Server", templateUrl: "html/serverInfo.html", Nav: true } )
$routeProvider.when( "/:address/console", { Title: "Console", templateUrl: "html/console.html", Nav: true } )
$routeProvider.when( "/:address/chat", { Title: "Chat", templateUrl: "html/chat.html", Nav: true } )
$routeProvider.when( "/:address/playerlist", { Title: "Player List", templateUrl: "html/playerlist.html", Nav: true } )
$routeProvider.when( "/:address/player/:userid", { Title: "Player Info", templateUrl: "html/playerInfo.html" } )
$routeProvider.when( "/:address/player/:userid/xp", { Title: "Player Xp Stats", templateUrl: "html/playerXp.html" } )
$routeProvider.otherwise( { redirectTo: '/home' } );
} );
app.controller( 'RconController', RconController );
function RconController( $scope, $rootScope, rconService, $timeout, $route )
{
function RconController($scope, $rootScope, rconService, $timeout, $route) {
$scope.$route = $route;
$scope.pages = $.map( $route.routes, function ( value, index )
{
$scope.pages = $.map($route.routes, function(value, index) {
if (value.Nav) {
return [value];
} );
}
});
$scope.OpenLeftMenu = function ()
{
$mdSidenav( 'left' ).toggle();
$scope.OpenLeftMenu = function() {
$mdSidenav('left').toggle();
};
$scope.IsConnected = function ()
{
$scope.IsConnected = function() {
return rconService.IsConnected();
}
$rootScope.Nav = function ( url )
{
return url.replace( ":address", rconService.Address );
$rootScope.Nav = function(url) {
return url.replace(":address", rconService.Address);
}
$rootScope.$on( '$stateChangeStart', function ( next, current )
{
console.log( next );
} );
$rootScope.$on('$stateChangeStart', function(next, current) {
console.log(next);
});
rconService.OnOpen = function ()
{
rconService.OnOpen = function() {
$scope.Connected = true;
$scope.$broadcast( "OnConnected" );
$scope.$broadcast("OnConnected");
$scope.$digest();
$scope.address = rconService.Address;
}
rconService.OnClose = function ( ev )
{
$scope.$broadcast( "OnDisconnected", ev );
rconService.OnClose = function(ev) {
$scope.$broadcast("OnDisconnected", ev);
$scope.$digest();
}
rconService.OnError = function ( ev )
{
$scope.$broadcast( "OnConnectionError", ev );
rconService.OnError = function(ev) {
$scope.$broadcast("OnConnectionError", ev);
$scope.$digest();
}
rconService.OnMessage = function ( msg )
{
$scope.$apply( function ()
{
$scope.$broadcast( "OnMessage", msg );
} );
rconService.OnMessage = function(msg) {
$scope.$apply(function() {
$scope.$broadcast("OnMessage", msg);
});
}
$scope.Disconnect = function() {
if (confirm('Do you really want to disconnect?')) {
rconService.Disconnect();
$scope.Connected = false;
$scope.address = '#/home';
}
}
}
app.filter( 'SecondsToDuration', [SecondsToDuration] );
app.filter('SecondsToDuration', [SecondsToDuration]);
function SecondsToDuration()
{
return function ( input )
{
input = parseInt( input );
function SecondsToDuration() {
return function(input) {
input = parseInt(input);
var out = "";
var hours = Math.floor( input / 3600 );
if ( input > 3600 ) out += hours + "h";
var hours = Math.floor(input / 3600);
if (input > 3600)
out += hours + "h";
var minutes = Math.floor( input / 60 ) % 60;
if ( input > 60 ) out += minutes + "m";
var minutes = Math.floor(input / 60) % 60;
if (input > 60)
out += minutes + "m";
var seconds = input % 60;
out += seconds + "s";
+69 -61
View File
@@ -1,34 +1,39 @@
function RconService() {
function RconService()
{
var s = {
var ConnectionStatus = {
'CONNECTING': 0,
'OPEN': 1,
'CLOSING': 2,
'CLOSED': 3
};
var Service = {
Socket: null,
Address: null,
Callbacks: {}
};
s.Connect = function ( addr, pass )
{
s.Socket = new WebSocket( "ws://" + addr + "/" + pass );
s.Address = addr;
var LastIndex = 1001;
s.Socket.onmessage = function ( e )
{
var data = angular.fromJson( e.data );
Service.Connect = function(addr, pass) {
this.Socket = new WebSocket("ws://" + addr + "/" + pass);
this.Address = addr;
this.Socket.onmessage = function(e) {
var data = angular.fromJson(e.data);
//
// This is a targetted message, it has an identifier
// So feed it back to the right callback.
//
if ( data.Identifier > 1000 )
{
var cb = s.Callbacks[data.Identifier];
if ( cb != null )
{
cb.scope.$apply( function ()
{
cb.callback( data );
} );
if (data.Identifier > 1000) {
var cb = Service.Callbacks[data.Identifier];
if (cb != null) {
cb.scope.$apply(function() {
cb.callback(data);
});
}
s.Callbacks[data.Identifier] = null;
Service.Callbacks[data.Identifier] = null;
return;
}
@@ -36,57 +41,64 @@ function RconService()
//
// Generic console message, let OnMessage catch it
//
if ( s.OnMessage != null )
{
s.OnMessage( data );
if (Service.OnMessage != null) {
Service.OnMessage(data);
}
};
s.Socket.onopen = s.OnOpen;
s.Socket.onclose = s.OnClose;
s.Socket.onerror = s.OnError;
this.Socket.onopen = this.OnOpen;
this.Socket.onclose = this.OnClose;
this.Socket.onerror = this.OnError;
}
s.Command = function ( msg, identifier )
{
if ( identifier == null )
Service.Disconnect = function() {
if (this.Socket) {
this.Socket.close();
this.Socket = null;
}
this.Callbacks = {};
}
Service.Command = function(msg, identifier) {
if (this.Socket === null)
return;
if (!this.IsConnected())
return;
if (identifier === null)
identifier = -1;
if ( s.Socket == null )
return;
if ( !s.IsConnected )
return;
var packet =
{
var packet = {
Identifier: identifier,
Message: msg,
Name: "WebRcon"
}
s.Socket.send( JSON.stringify( packet ) );
};
var LastIndex = 1001;
this.Socket.send(JSON.stringify(packet));
};
//
// Make a request, call this function when it returns
//
s.Request = function ( msg, scope, callback )
{
Service.Request = function(msg, scope, callback) {
LastIndex++;
s.Callbacks[LastIndex] = { scope: scope, callback: callback };
s.Command( msg, LastIndex );
this.Callbacks[LastIndex] = {
scope: scope,
callback: callback
};
Service.Command(msg, LastIndex);
}
//
// Returns true if websocket is connected
//
s.IsConnected = function ()
{
if ( s.Socket == null ) return false;
return s.Socket.readyState == 1;
Service.IsConnected = function() {
if (this.Socket == null)
return false;
return this.Socket.readyState === ConnectionStatus.OPEN;
}
//
@@ -95,29 +107,25 @@ function RconService()
// Basically if not connected, call this function when we are
// And if we are - then call it right now.
//
s.InstallService = function( scope, func )
{
scope.$on( "OnConnected", function ()
{
Service.InstallService = function(scope, func) {
scope.$on("OnConnected", function() {
func();
});
if ( s.IsConnected() )
{
if (this.IsConnected()) {
func();
}
}
s.getPlayers = function(scope, success) {
this.Request( "playerlist", scope, function ( response )
{
var players = JSON.parse( response.Message );
Service.getPlayers = function(scope, success) {
this.Request("playerlist", scope, function(response) {
var players = JSON.parse(response.Message);
if(typeof success === 'function') {
if (typeof success === 'function') {
success.call(scope, players);
}
});
}
return s;
return Service;
}
+15
View File
@@ -0,0 +1,15 @@
{
"name": "webrcon",
"description": "Game server Rcon, using websockets",
"repository": {
"type": "git",
"url": "https://github.com/Facepunch/webrcon"
},
"scripts": {
"server": "http-server -p 8888",
"start": "npm run server"
},
"devDependencies": {
"http-server": "^0.9.0"
}
}
+1
View File
@@ -118,6 +118,7 @@ a:hover
color: #fff;
}
.navbar-default .navbar-nav > li > .navbar-text,
.navbar-default .navbar-nav > li > a, .navbar-default .navbar-nav > li > a:hover, .navbar-default .navbar-nav > li > a:focus
{
color: #fff;