export class SocketManager { constructor (app) { this._app = app this._hasRequestedHistoryGraph = false this._reconnectDelayBase = 0 } reset () { this._hasRequestedHistoryGraph = false } createWebSocket () { let webSocketProtocol = 'ws:' if (location.protocol === 'https:') { webSocketProtocol = 'wss:' } this._webSocket = new WebSocket(`${webSocketProtocol}//${location.host}`) // The backend will automatically push data once connected this._webSocket.onopen = () => { this._app.caption.set('Caricamento...') // Reset reconnection scheduling since the WebSocket has been established this._reconnectDelayBase = 0 } this._webSocket.onclose = (event) => { this._app.handleDisconnect() // Modify page state to display loading overlay // Code 1006 denotes "Abnormal closure", most likely from the server or client losing connection // See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent // Treat other codes as active errors (besides connectivity errors) when displaying the message if (event.code === 1006) { this._app.caption.set('Connessione persa!') } else { this._app.caption.set('Disconnesso per un errore.') } // Schedule socket reconnection attempt this.scheduleReconnect() } this._webSocket.onmessage = (message) => { const payload = JSON.parse(message.data) switch (payload.message) { case 'init': this._app.setPublicConfig(payload.config) // Display the main page component // Called here instead of syncComplete so the DOM can be drawn prior to the graphs being drawn this._app.setPageReady(true) // Allow the graphDisplayManager to control whether or not the historical graph is loaded // Defer to isGraphVisible from the publicConfig to understand if the frontend will ever receive a graph payload if (this._app.publicConfig.isGraphVisible) { this.sendHistoryGraphRequest() } payload.servers.forEach((serverPayload, serverId) => { this._app.addServer(serverId, serverPayload, payload.timestampPoints) }) // Init payload contains all data needed to render the page // Alert the app it is ready this._app.handleSyncComplete() break case 'updateServers': { for (let serverId = 0; serverId < payload.updates.length; serverId++) { // The backend may send "update" events prior to receiving all "add" events // A server has only been added once it's ServerRegistration is defined // Checking undefined protects from this race condition const serverRegistration = this._app.serverRegistry.getServerRegistration(serverId) const serverUpdate = payload.updates[serverId] if (serverRegistration) { serverRegistration.handlePing(serverUpdate, payload.timestamp) serverRegistration.updateServerStatus(serverUpdate, this._app.publicConfig.minecraftVersions) } } // Bulk add playerCounts into graph during #updateHistoryGraph if (payload.updateHistoryGraph) { this._app.graphDisplayManager.addGraphPoint(payload.timestamp, Object.values(payload.updates).map(update => update.playerCount)) // Run redraw tasks after handling bulk updates this._app.graphDisplayManager.redraw() } this._app.percentageBar.redraw() this._app.updateGlobalStats() break } case 'historyGraph': { this._app.graphDisplayManager.buildPlotInstance(payload.timestamps, payload.graphData) // Build checkbox elements for graph controls let lastRowCounter = 0 let controlsHTML = '' this._app.serverRegistry.getServerRegistrations() .map(serverRegistration => serverRegistration.data.name) .sort() .forEach(serverName => { const serverRegistration = this._app.serverRegistry.getServerRegistration(serverName) controlsHTML += `