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
+111 -104
View File
@@ -1,123 +1,130 @@
function RconService() {
var ConnectionStatus = {
'CONNECTING': 0,
'OPEN': 1,
'CLOSING': 2,
'CLOSED': 3
};
function RconService()
{
var s = {
Callbacks: {}
};
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 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 );
} );
}
s.Callbacks[data.Identifier] = null;
this.Socket.onmessage = function(e) {
var data = angular.fromJson(e.data);
return;
}
//
// This is a targetted message, it has an identifier
// So feed it back to the right callback.
//
if (data.Identifier > 1000) {
var cb = Service.Callbacks[data.Identifier];
if (cb != null) {
cb.scope.$apply(function() {
cb.callback(data);
});
}
Service.Callbacks[data.Identifier] = null;
//
// Generic console message, let OnMessage catch it
//
if ( s.OnMessage != null )
{
s.OnMessage( data );
}
};
return;
}
s.Socket.onopen = s.OnOpen;
s.Socket.onclose = s.OnClose;
s.Socket.onerror = s.OnError;
}
//
// Generic console message, let OnMessage catch it
//
if (Service.OnMessage != null) {
Service.OnMessage(data);
}
};
s.Command = function ( msg, identifier )
{
if ( identifier == null )
identifier = -1;
this.Socket.onopen = this.OnOpen;
this.Socket.onclose = this.OnClose;
this.Socket.onerror = this.OnError;
}
if ( s.Socket == null )
return;
if ( !s.IsConnected )
return;
Service.Disconnect = function() {
if (this.Socket) {
this.Socket.close();
this.Socket = null;
}
var packet =
{
Identifier: identifier,
Message: msg,
Name: "WebRcon"
}
this.Callbacks = {};
}
s.Socket.send( JSON.stringify( packet ) );
};
Service.Command = function(msg, identifier) {
if (this.Socket === null)
return;
var LastIndex = 1001;
if (!this.IsConnected())
return;
//
// Make a request, call this function when it returns
//
s.Request = function ( msg, scope, callback )
{
LastIndex++;
s.Callbacks[LastIndex] = { scope: scope, callback: callback };
s.Command( msg, LastIndex );
}
if (identifier === null)
identifier = -1;
//
// Returns true if websocket is connected
//
s.IsConnected = function ()
{
if ( s.Socket == null ) return false;
return s.Socket.readyState == 1;
}
var packet = {
Identifier: identifier,
Message: msg,
Name: "WebRcon"
};
//
// Helper for installing connectivity logic
//
// 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 ()
{
func();
});
this.Socket.send(JSON.stringify(packet));
};
if ( s.IsConnected() )
{
func();
}
}
//
// Make a request, call this function when it returns
//
Service.Request = function(msg, scope, callback) {
LastIndex++;
this.Callbacks[LastIndex] = {
scope: scope,
callback: callback
};
Service.Command(msg, LastIndex);
}
s.getPlayers = function(scope, success) {
this.Request( "playerlist", scope, function ( response )
{
var players = JSON.parse( response.Message );
//
// Returns true if websocket is connected
//
Service.IsConnected = function() {
if (this.Socket == null)
return false;
if(typeof success === 'function') {
success.call(scope, players);
}
});
}
return this.Socket.readyState === ConnectionStatus.OPEN;
}
return s;
}
//
// Helper for installing connectivity logic
//
// Basically if not connected, call this function when we are
// And if we are - then call it right now.
//
Service.InstallService = function(scope, func) {
scope.$on("OnConnected", function() {
func();
});
if (this.IsConnected()) {
func();
}
}
Service.getPlayers = function(scope, success) {
this.Request("playerlist", scope, function(response) {
var players = JSON.parse(response.Message);
if (typeof success === 'function') {
success.call(scope, players);
}
});
}
return Service;
}