summaryrefslogtreecommitdiff
path: root/WebInterface/src/js/modules/networking/networker.js
diff options
context:
space:
mode:
Diffstat (limited to 'WebInterface/src/js/modules/networking/networker.js')
-rw-r--r--WebInterface/src/js/modules/networking/networker.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/WebInterface/src/js/modules/networking/networker.js b/WebInterface/src/js/modules/networking/networker.js
new file mode 100644
index 0000000..199adc7
--- /dev/null
+++ b/WebInterface/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.Trace);
+ } 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();
+ }
+}