summaryrefslogtreecommitdiff
path: root/WebInterface/NodeJSServer/src/js/modules/networking/networker.js
diff options
context:
space:
mode:
authorTrueKuehli <rctcoaster2000@hotmail.de>2018-11-27 12:16:33 +0100
committerTrueKuehli <rctcoaster2000@hotmail.de>2018-11-27 12:16:33 +0100
commit9f0b255f32dfa81bffe75f89335a78a659b4ce6a (patch)
treec4345ef6f98fee35f326bc06c29cada542a78e43 /WebInterface/NodeJSServer/src/js/modules/networking/networker.js
parenta653a4efc60ef0bbc18e65cb11a4bd8c06c7ad5c (diff)
Reworked the code, but currently unable to test, so bugs are bound to be in there
Will test it sometime later. There also might still be stuff, that has yet to be reworked.
Diffstat (limited to 'WebInterface/NodeJSServer/src/js/modules/networking/networker.js')
-rw-r--r--WebInterface/NodeJSServer/src/js/modules/networking/networker.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/WebInterface/NodeJSServer/src/js/modules/networking/networker.js b/WebInterface/NodeJSServer/src/js/modules/networking/networker.js
new file mode 100644
index 0000000..7667895
--- /dev/null
+++ b/WebInterface/NodeJSServer/src/js/modules/networking/networker.js
@@ -0,0 +1,89 @@
+import * as signalR from '@aspnet/signalr';
+import LoginCommands from './commands/loginCmds';
+import PlayCommands from './commands/playCmds';
+
+/**
+ * Class for communication to server
+ */
+export default class Networker {
+ /**
+ * Creates new Networker and connects it to the Interface
+ * @param {Interface} iface Interface for communication between objects
+ * @param {String} url URL of the server backend
+ * @param {Boolean} [debug=false] Should there be debug output
+ */
+ constructor(iface, url, debug = false) {
+ this.url = url;
+
+ // Register in Interface
+ iface.addObject(this, 'networker',
+ ['sendRequest', 'registerHandler', 'removeHandler']);
+ this.iface = iface;
+
+ const connectionBuilder = new signalR.HubConnectionBuilder()
+ .withUrl(url);
+
+ if (debug) {
+ connectionBuilder.configureLogging(signalR.LogLevel.Debug);
+ } else {
+ connectionBuilder.configureLogging(signalR.LogLevel.Error);
+ }
+
+ this.connection = connectionBuilder.build();
+ this.connection.start()
+ .then(() => this.iface.callMethod('listServers', 'listServers'))
+ .catch((err) => console.error(err.toString()));
+
+ // Initialize refreshing (blocks new refreshes if true)
+ this.refreshing = false;
+ }
+
+ /**
+ * Sending a network request to the server
+ * @param {String} methodName Method to call on server
+ * @param {function} errorHandler Function to call on error
+ * @param {...*} args Arguments to pass to server
+ */
+ sendRequest(methodName, errorHandler, ...args) {
+ this.connection.invoke(methodName, ...args).catch(errorHandler);
+ }
+
+ /**
+ * Register a new function to be called upon receival of message from server
+ * @param {String} name Name of invoked method
+ * @param {function} fn function to call with received data
+ */
+ registerHandler(name, fn) {
+ this.connection.on(name, fn);
+ }
+
+ /**
+ * Removes handler for receiving messages from the server
+ * @param {String} name Name of the invoked method
+ */
+ removeHandler(name) {
+ this.connection.off(name);
+ }
+
+ /**
+ * Initializes Login Commands
+ */
+ initLogin() {
+ this.loginCmd = new LoginCommands(this.iface);
+ }
+
+ /**
+ * Initializes play commands
+ */
+ initPlay() {
+ this.playCmd = new PlayCommands(this.iface);
+ }
+
+ /**
+ * Clears all currently registered commands
+ */
+ clearCommands() {
+ if (this.loginCmd) this.loginCmd.destroy();
+ if (this.playCmd) this.playCmd.destroy();
+ }
+}