blob: 199adc786ba15fe367307e70ef15b7b0d2ee26e1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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();
}
}
|