From a056fac1c1f7851d9629e3ca794ef540daa529bf Mon Sep 17 00:00:00 2001 From: Rafostar <40623528+Rafostar@users.noreply.github.com> Date: Tue, 15 Dec 2020 22:35:14 +0100 Subject: [PATCH] Add logic responsible for starting web app --- clapper_src/playerBase.js | 28 ++++++++++++++++---- clapper_src/playerRemote.js | 5 +--- clapper_src/prefs.js | 11 ++++---- clapper_src/webClient.js | 12 ++++++--- clapper_src/webServer.js | 2 +- data/com.github.rafostar.Clapper.gschema.xml | 4 +++ 6 files changed, 42 insertions(+), 20 deletions(-) diff --git a/clapper_src/playerBase.js b/clapper_src/playerBase.js index facb9ba8..1dc851fc 100644 --- a/clapper_src/playerBase.js +++ b/clapper_src/playerBase.js @@ -1,6 +1,7 @@ const { Gio, GLib, GObject, Gst, GstPlayer, Gtk } = imports.gi; const Debug = imports.clapper_src.debug; const Misc = imports.clapper_src.misc; +const { WebApp } = imports.clapper_src.webApp; let WebServer; @@ -69,6 +70,7 @@ class ClapperPlayerBase extends GstPlayer.Player this.visualization_enabled = false; this.webserver = null; + this.webapp = null; this.set_all_plugins_ranks(); this.set_initial_config(); @@ -93,7 +95,7 @@ class ClapperPlayerBase extends GstPlayer.Player 'audio-offset', 'subtitle-offset', 'play-flags', - 'webserver-enabled', + 'webserver-enabled' ]; for(let key of settingsToSet) @@ -286,7 +288,8 @@ class ClapperPlayerBase extends GstPlayer.Player debug(`changed play flags: ${initialFlags} -> ${settingsFlags}`); break; case 'webserver-enabled': - const webserverEnabled = settings.get_boolean(key); + case 'webapp-enabled': + const webserverEnabled = settings.get_boolean('webserver-enabled'); if(webserverEnabled) { if(!WebServer) { @@ -295,13 +298,28 @@ class ClapperPlayerBase extends GstPlayer.Player WebServer = imports.clapper_src.webServer.WebServer; } - if(!this.webserver) + if(!this.webserver) { this.webserver = new WebServer(settings.get_int('webserver-port')); - + this.webserver.passMsgData = this.receiveWs.bind(this); + } this.webserver.startListening(); - this.webserver.passMsgData = this.receiveWs.bind(this); + + const webappEnabled = settings.get_boolean('webapp-enabled'); + + if(!this.webapp && !webappEnabled) + break; + + if(webappEnabled) { + if(!this.webapp) + this.webapp = new WebApp(); + + this.webapp.startRemoteApp(); + } + else + this.webapp.stopRemoteApp(); } else if(this.webserver) { + /* remote app will close too when connection is lost */ this.webserver.stopListening(); } break; diff --git a/clapper_src/playerRemote.js b/clapper_src/playerRemote.js index 6a5b4c4d..166dd18c 100644 --- a/clapper_src/playerRemote.js +++ b/clapper_src/playerRemote.js @@ -1,9 +1,6 @@ const { GObject } = imports.gi; -const Misc = imports.clapper_src.misc; const { WebClient } = imports.clapper_src.webClient; -let { settings } = Misc; - var PlayerRemote = GObject.registerClass( class ClapperPlayerRemote extends GObject.Object { @@ -11,7 +8,7 @@ class ClapperPlayerRemote extends GObject.Object { super._init(); - this.webclient = new WebClient(settings.get_int('webserver-port')); + this.webclient = new WebClient(); } set_playlist(playlist) diff --git a/clapper_src/prefs.js b/clapper_src/prefs.js index 634647e5..57bf7cd5 100644 --- a/clapper_src/prefs.js +++ b/clapper_src/prefs.js @@ -95,16 +95,15 @@ class ClapperNetworkPage extends PrefsBase.Grid { super._init(); - let checkButton; - let spinButton; - this.addTitle('Client'); this.addPlayFlagCheckButton('Progressive download buffering', Gst.PlayFlags.DOWNLOAD); this.addTitle('Server'); - checkButton = this.addCheckButton('Allow remote control of the player', 'webserver-enabled'); - spinButton = this.addSpinButton('Listening port', 1024, 65535, 'webserver-port'); - checkButton.bind_property('active', spinButton, 'visible', GObject.BindingFlags.SYNC_CREATE); + let webServer = this.addCheckButton('Control player remotely', 'webserver-enabled'); + let serverPort = this.addSpinButton('Listening port', 1024, 65535, 'webserver-port'); + webServer.bind_property('active', serverPort, 'visible', GObject.BindingFlags.SYNC_CREATE); + //let webApp = this.addCheckButton('Run built-in web application', 'webapp-enabled'); + //webServer.bind_property('active', webApp, 'visible', GObject.BindingFlags.SYNC_CREATE); } }); diff --git a/clapper_src/webClient.js b/clapper_src/webClient.js index 938586dc..168db203 100644 --- a/clapper_src/webClient.js +++ b/clapper_src/webClient.js @@ -1,8 +1,10 @@ -const { Soup, GObject } = imports.gi; +const { Gio, GObject, Soup } = imports.gi; const Debug = imports.clapper_src.debug; +const Misc = imports.clapper_src.misc; const WebHelpers = imports.clapper_src.webHelpers; let { debug } = Debug; +let { settings } = Misc; var WebClient = GObject.registerClass( class ClapperWebClient extends Soup.Session @@ -16,15 +18,16 @@ class ClapperWebClient extends Soup.Session this.wsConn = null; - this.connectWebsocket(port); + this.connectWebsocket(); } - connectWebsocket(port) + connectWebsocket() { if(this.wsConn) return; - let message = Soup.Message.new('GET', `ws://127.0.0.1:${port}/websocket`); + const port = settings.get_int('webserver-port'); + const message = Soup.Message.new('GET', `ws://127.0.0.1:${port}/websocket`); this.websocket_connect_async(message, null, null, null, this._onWsConnect.bind(this)); debug('connecting WebSocket to Clapper app'); @@ -78,6 +81,7 @@ class ClapperWebClient extends Soup.Session _onWsClosed(connection) { debug('closed WebSocket connection'); + this.wsConn = null; this.passMsgData('close'); } diff --git a/clapper_src/webServer.js b/clapper_src/webServer.js index 72895564..8e30f408 100644 --- a/clapper_src/webServer.js +++ b/clapper_src/webServer.js @@ -55,7 +55,7 @@ class ClapperWebServer extends Soup.Server if(isListening) { const uris = this.get_uris(); const usedPort = uris[0].get_port(); - debug(`WebSocket server listening on port: ${usedPort}`); + debug(`WebSocket server started listening on port: ${usedPort}`); } else { debug(new Error('WebSocket server could not start listening')); diff --git a/data/com.github.rafostar.Clapper.gschema.xml b/data/com.github.rafostar.Clapper.gschema.xml index 15f85b95..24a346db 100644 --- a/data/com.github.rafostar.Clapper.gschema.xml +++ b/data/com.github.rafostar.Clapper.gschema.xml @@ -58,6 +58,10 @@ 6446 Listening port to use for incoming WebSocket connections + + false + Run built-in broadway based web application +