From 9f0b255f32dfa81bffe75f89335a78a659b4ce6a Mon Sep 17 00:00:00 2001 From: TrueKuehli Date: Tue, 27 Nov 2018 12:16:33 +0100 Subject: 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. --- .../src/js/modules/networking/networker.js | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 WebInterface/NodeJSServer/src/js/modules/networking/networker.js (limited to 'WebInterface/NodeJSServer/src/js/modules/networking/networker.js') 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(); + } +} -- cgit v1.2.3-54-g00ecf