add Disconnect

added disconnect feature
auto formatted js/html code
This commit is contained in:
alexfriesen
2017-04-21 15:06:52 +02:00
parent cbe2b27e28
commit a02a57bf99
4 changed files with 289 additions and 241 deletions
+39 -11
View File
@@ -1,4 +1,4 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" ng-app="RconApp"> <html lang="en" ng-app="RconApp">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
@@ -12,17 +12,42 @@
</head> </head>
<body ng-controller="RconController"> <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="container">
<div class="navbar-header"> <div class="navbar-header">
<a class="navbar-brand" href="#">{{info.Hostname}}</a> <a class="navbar-brand" href="#">{{info.Hostname}}</a>
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li><div class="navbar-info" title="Uptime"><span class="glyphicon glyphicon-time"></span> {{info.Uptime|SecondsToDuration}}</div></li> <li>
<li><div class="navbar-info" title="Map Name"><span class="glyphicon glyphicon-globe"></span> {{info.Map}}</div></li> <div class="navbar-info" title="Uptime">
<li><div class="navbar-info" title="Current Players/Maximum Players"><span class="glyphicon glyphicon-user"></span> {{info.Players|number}}/{{info.MaxPlayers|number}}</div></li> <span class="glyphicon glyphicon-time"></span>
<li><div class="navbar-info" title="Framerate"><span class="glyphicon glyphicon-hourglass"></span> {{info.Framerate|number}}fps</div></li> {{info.Uptime|SecondsToDuration}}
<li><div class="navbar-info" title="Entity Count"><span class="glyphicon glyphicon-oil"></span> {{info.EntityCount|number}}</div></li> </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>
</ul> </ul>
</div> </div>
@@ -32,14 +57,16 @@
<div class="container"> <div class="container">
<div ng-include="'html/connect.html'" ng-show="!Connected"> <div ng-include="'html/connect.html'" ng-show="!Connected"></div>
</div>
<div ng-show="Connected" class="col-sm-2 sidebar"> <div ng-show="Connected" class="col-sm-2 sidebar">
<ul class="nav nav-sidebar"> <ul class="nav nav-sidebar">
<li ng-repeat="p in pages | filter: { Nav: '' } " ng-class="{'active' : $route.current.Title == p.Title }"> <li ng-repeat="page in pages" ng-class="{'active' : $route.current.Title == page.Title }">
<a href="#{{Nav( p.originalPath )}}">{{p.Title}}</a> <a href="#{{Nav( page.originalPath )}}">{{page.Title}}</a>
</li>
<li>
<a href="#" ng-click="Disconnect()">Disconnect</a>
</li> </li>
</ul> </ul>
@@ -49,6 +76,7 @@
<h1 class="page-header">{{$route.current.Title}}</h1> <h1 class="page-header">{{$route.current.Title}}</h1>
<div class="row" ng-view></div> <div class="row" ng-view></div>
</div> </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 ) function RconController($scope, $rootScope, rconService, $timeout, $route) {
{
$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 )
{
$scope.$route = $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]; return [value];
} ); }
});
$scope.OpenLeftMenu = function () $scope.OpenLeftMenu = function() {
{ $mdSidenav('left').toggle();
$mdSidenav( 'left' ).toggle();
}; };
$scope.IsConnected = function () $scope.IsConnected = function() {
{
return rconService.IsConnected(); return rconService.IsConnected();
} }
$rootScope.Nav = function ( url ) $rootScope.Nav = function(url) {
{ return url.replace(":address", rconService.Address);
return url.replace( ":address", rconService.Address );
} }
$rootScope.$on( '$stateChangeStart', function ( next, current ) $rootScope.$on('$stateChangeStart', function(next, current) {
{ console.log(next);
console.log( next ); });
} );
rconService.OnOpen = function () rconService.OnOpen = function() {
{
$scope.Connected = true; $scope.Connected = true;
$scope.$broadcast( "OnConnected" ); $scope.$broadcast("OnConnected");
$scope.$digest(); $scope.$digest();
$scope.address = rconService.Address; $scope.address = rconService.Address;
} }
rconService.OnClose = function ( ev ) rconService.OnClose = function(ev) {
{ $scope.$broadcast("OnDisconnected", ev);
$scope.$broadcast( "OnDisconnected", ev );
$scope.$digest(); $scope.$digest();
} }
rconService.OnError = function ( ev ) rconService.OnError = function(ev) {
{ $scope.$broadcast("OnConnectionError", ev);
$scope.$broadcast( "OnConnectionError", ev );
$scope.$digest(); $scope.$digest();
} }
rconService.OnMessage = function ( msg ) rconService.OnMessage = function(msg) {
{ $scope.$apply(function() {
$scope.$apply( function () $scope.$broadcast("OnMessage", msg);
{ });
$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() function SecondsToDuration() {
{ return function(input) {
return function ( input ) input = parseInt(input);
{
input = parseInt( input );
var out = ""; var out = "";
var hours = Math.floor( input / 3600 ); var hours = Math.floor(input / 3600);
if ( input > 3600 ) out += hours + "h"; if (input > 3600)
out += hours + "h";
var minutes = Math.floor( input / 60 ) % 60; var minutes = Math.floor(input / 60) % 60;
if ( input > 60 ) out += minutes + "m"; if (input > 60)
out += minutes + "m";
var seconds = input % 60; var seconds = input % 60;
out += seconds + "s"; out += seconds + "s";
+68 -61
View File
@@ -1,34 +1,38 @@
function RconService() {
var ConnectionStatus = {
'CONNECTING': 0,
'OPEN': 1,
'CLOSING': 2,
'CLOSED': 3
};
function RconService() var Service = {
{ Socket: null,
var s = { Address: null,
Callbacks: {} Callbacks: {}
}; };
s.Connect = function ( addr, pass ) var LastIndex = 1001;
{
s.Socket = new WebSocket( "ws://" + addr + "/" + pass );
s.Address = addr;
s.Socket.onmessage = function ( e ) Service.Connect = function(addr, pass) {
{ this.Socket = new WebSocket("ws://" + addr + "/" + pass);
var data = angular.fromJson( e.data ); this.Address = addr;
this.Socket.onmessage = function(e) {
var data = angular.fromJson(e.data);
// //
// This is a targetted message, it has an identifier // This is a targetted message, it has an identifier
// So feed it back to the right callback. // So feed it back to the right callback.
// //
if ( data.Identifier > 1000 ) if (data.Identifier > 1000) {
{ var cb = Service.Callbacks[data.Identifier];
var cb = s.Callbacks[data.Identifier]; if (cb != null) {
if ( cb != null ) cb.scope.$apply(function() {
{ cb.callback(data);
cb.scope.$apply( function () });
{
cb.callback( data );
} );
} }
s.Callbacks[data.Identifier] = null; Service.Callbacks[data.Identifier] = null;
return; return;
} }
@@ -36,57 +40,64 @@ function RconService()
// //
// Generic console message, let OnMessage catch it // Generic console message, let OnMessage catch it
// //
if ( s.OnMessage != null ) if (Service.OnMessage != null) {
{ Service.OnMessage(data);
s.OnMessage( data );
} }
}; };
s.Socket.onopen = s.OnOpen; this.Socket.onopen = this.OnOpen;
s.Socket.onclose = s.OnClose; this.Socket.onclose = this.OnClose;
s.Socket.onerror = s.OnError; this.Socket.onerror = this.OnError;
} }
s.Command = function ( msg, identifier ) Service.Disconnect = function() {
{ if (this.Socket) {
if ( identifier == null ) 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; identifier = -1;
if ( s.Socket == null ) var packet = {
return;
if ( !s.IsConnected )
return;
var packet =
{
Identifier: identifier, Identifier: identifier,
Message: msg, Message: msg,
Name: "WebRcon" 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 // Make a request, call this function when it returns
// //
s.Request = function ( msg, scope, callback ) Service.Request = function(msg, scope, callback) {
{
LastIndex++; LastIndex++;
s.Callbacks[LastIndex] = { scope: scope, callback: callback }; this.Callbacks[LastIndex] = {
s.Command( msg, LastIndex ); scope: scope,
callback: callback
};
Service.Command(msg, LastIndex);
} }
// //
// Returns true if websocket is connected // Returns true if websocket is connected
// //
s.IsConnected = function () Service.IsConnected = function() {
{ if (this.Socket == null)
if ( s.Socket == null ) return false; return false;
return s.Socket.readyState == 1;
return this.Socket.readyState === ConnectionStatus.OPEN;
} }
// //
@@ -95,29 +106,25 @@ function RconService()
// Basically if not connected, call this function when we are // Basically if not connected, call this function when we are
// And if we are - then call it right now. // And if we are - then call it right now.
// //
s.InstallService = function( scope, func ) Service.InstallService = function(scope, func) {
{ scope.$on("OnConnected", function() {
scope.$on( "OnConnected", function ()
{
func(); func();
}); });
if ( s.IsConnected() ) if (this.IsConnected()) {
{
func(); func();
} }
} }
s.getPlayers = function(scope, success) { Service.getPlayers = function(scope, success) {
this.Request( "playerlist", scope, function ( response ) this.Request("playerlist", scope, function(response) {
{ var players = JSON.parse(response.Message);
var players = JSON.parse( response.Message );
if(typeof success === 'function') { if (typeof success === 'function') {
success.call(scope, players); success.call(scope, players);
} }
}); });
} }
return s; return Service;
} }