Added wrapper

This commit is contained in:
Carlos 2020-12-12 13:52:36 +01:00
parent 383b0e3420
commit 7609179e24
4 changed files with 214 additions and 2 deletions

103
API_js/WebConsoleAPI.js Normal file
View File

@ -0,0 +1,103 @@
/**
WebConsole API for WebConsole
Used to manage active connections
https://github.com/mesacarlos
2019-2020 Carlos Mesa under MIT License.
*/
class WebConsoleAPI {
constructor(serverURI){
this.activeConnection = new WebConsoleConnector(serverURI);
this.activeConnection.subscribe(this.onMsg);
this.activeConnection.connect();
}
/**
* Send password to server
*/
login(pwd){
this.activeConnection.sendToServer({
command: "LOGIN",
params: pwd
});
}
/**
* Send console command to server
*/
sendConsoleCmd(cmd){
this.activeConnection.sendToServer({
command: "EXEC",
token: this.activeConnection.token,
params: cmd
});
this.activeConnection.commands.push(cmd);
}
/**
* Asks server for CPU, RAM and players info
*/
askForInfo(){
this.activeConnection.sendToServer({
command: "PLAYERS",
token: this.activeConnection.token,
});
this.activeConnection.sendToServer({
command: "CPUUSAGE",
token: this.activeConnection.token,
});
this.activeConnection.sendToServer({
command: "RAMUSAGE",
token: this.activeConnection.token,
});
}
/**
* Asks server for full latest.log
*/
askForLogs(){
this.activeConnection.sendToServer({
command: "READLOGFILE",
token: this.activeConnection.token,
});
}
/**
* This function is executed when a message is received from the server
*/
onMsg(message){
//Print JSON to console. You may remove this line:
console.log(message);
//Type your code inside the switch cases:
switch (message.status) {
case 10:
//Console Output
//Info: The weird characters you probably get are the color indicators. Check the link below to learn how the official client parses them
//https://github.com/mesacarlos/WebConsole/blob/383b0e3420a948a61c7935ff84f40ff159fbd466/client/scripts/WebConsole.js#L128
break;
case 200:
//LoggedIn
break;
case 400:
//Unknown Command
break;
case 401:
//Waiting for login...
break;
case 1000:
//Connected players info
break;
case 1001:
//Cpu Usage info
break;
case 1002:
//RAM Usage info
break;
default:
console.log('Unknown server response:');
}
}
}

View File

@ -0,0 +1,96 @@
/**
WebConsole Connector for WebConsole
Used to connect to WebSocketsServer
https://github.com/mesacarlos
2019-2020 Carlos Mesa under MIT License.
*/
class WebConsoleConnector {
constructor(serverURI) {
this.serverURI = serverURI;
this.token;
this.subscribers = []; //List of functions called when a new message arrive
this.messages = []; //All messages retrieved since connection start
this.commands = []; //EXEC Commands sent by user to this server
this.isLogged = false; //Is logged in with valid pasword or not
}
/**
* Connect to WebSocket
*/
connect(){
var connector = this;
this.websocket = new WebSocket(this.serverURI);
this.websocket.onopen = function(evt) { connector.onOpen(evt) };
this.websocket.onclose = function(evt) { connector.onClose(evt) };
this.websocket.onmessage = function(evt) { connector.onMessage(evt) };
this.websocket.onerror = function(evt) { connector.onError(evt) };
}
/**
* Internal function
*/
onOpen(evt){
//Event: Connection opened
}
/**
* Internal function
*/
onClose(evt){
console.log("Closed reason: " + evt.reason); //No reason provided (using chrome at least)
}
/**
* Internal function
*/
onMessage(evt){
var obj = JSON.parse(evt.data);
//Sucessfully connected? Save token
if(obj.status === 200){
this.token = obj.token;
this.isLogged = true;
}
this.notify(obj); //Notify all subscribers
this.messages.push(obj);
}
/**
* Internal function
*/
onError(evt){
//Error occurred on the connection
}
/**
* Notifies a new message to all subscribers
*/
notify(obj){
this.subscribers.forEach(function(fun) {
fun(obj); //Calls function with this object
});
}
/**
* Sends a WebSocket command to Server
*/
sendToServer(message){
this.websocket.send(JSON.stringify(message));
}
/**
* Adds a function to subscriber list
*/
subscribe(func){
this.subscribers.push(func);
}
/**
* Unsubscribe all subscribers
*/
removeSubscribers(){
this.subscribers = [];
}
}

13
API_js/demo.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<script src="WebConsoleAPI.js"></script>
<script src="WebConsoleConnector.js"></script>
</head>
<body>
<h1>API demo</h1>
<p>Open your console (F12) and type: var webconsoleapi = new WebConsoleAPI("ws://localhost:8080");</p>
<p>Replace ws:// with wss:// if your server have SSL enabled, localhost with your server IP and 8080 with your port.</p>
<p>Then call login(pwd) to login, sendConsoleCmd(cmd) to execute a command, etc...</p>
</body>
</html>

View File

@ -42,7 +42,7 @@ Don't worry about privacy or security: all data is stored in your browser locall
A explanation of the `host`, `port`, `language` and `passwords` fields follows: A explanation of the `host`, `port`, `language` and `passwords` fields follows:
`host`: Leaving it as 0.0.0.0 will do the trick. If you experience issues , you can change this value to your device IP. If you are in a VPS or dedicated server (or you have a full public IP allocated for your device) type your public IP. If you are at your home (and you dont have a public IP assigned to your device) type your private IP, it is probably something like 192.168.xx.xx. `host`: Leaving it as 0.0.0.0 will do the trick. If you experience issues , you can change this value to your device IP. If you are in a VPS or dedicated server (or you have a full public IP allocated for your device) type your public IP. If you are at your home (and you don't have a public IP assigned to your device) type your private IP, it is probably something like 192.168.xx.xx.
`port`: A port where to run this plugin (cannot be the port you are using for Minecraft). `port`: A port where to run this plugin (cannot be the port you are using for Minecraft).
@ -54,7 +54,7 @@ From version 2.0 you can now create more than one user and set them as admin (Pe
- You can create as many admins as you want repeating this step. - You can create as many admins as you want repeating this step.
- If you want to create a view-only user, remove the `{}` after `viewer: ` and do the same process as for admin users below `viewer: `. You can also create as many viewers as needed. - If you want to create a view-only user, remove the `{}` after `viewer: ` and do the same process as for admin users below `viewer: `. You can also create as many viewers as needed.
The rest of the fields are used for SSL configuration. You can see a tutorial on how to activate SSL [in this link](https://github.com/mesacarlos/WebConsole/wiki/SSL-Configuration). SSL **is not** required for WebConsole to work, you can still use it without encription, unless you are hosting your client in a HTTPS server, in this case is mandatory to enable SSL in all your servers due to web browsers restrictions. The rest of the fields are used for SSL configuration. You can learn how to activate SSL [here](https://github.com/mesacarlos/WebConsole/wiki/SSL-Configuration). SSL **is not** required for WebConsole to work, you can still use it without encription, unless you are hosting your client in a HTTPS server, in this case is mandatory to enable SSL in all your servers due to web browsers restrictions.
## Using web interface ## Using web interface