From 5231a1f2251003e73aaf08537f758a718c32deab Mon Sep 17 00:00:00 2001 From: Rafostar <40623528+Rafostar@users.noreply.github.com> Date: Tue, 15 Dec 2020 18:20:48 +0100 Subject: [PATCH] Add initial WebSocket client app --- clapper_src/appRemote.js | 4 ++ clapper_src/playerRemote.js | 24 +++++++++++ clapper_src/webClient.js | 82 +++++++++++++++++++++++++++++++++++++ clapper_src/widgetRemote.js | 13 ++++++ 4 files changed, 123 insertions(+) create mode 100644 clapper_src/playerRemote.js create mode 100644 clapper_src/webClient.js create mode 100644 clapper_src/widgetRemote.js diff --git a/clapper_src/appRemote.js b/clapper_src/appRemote.js index ae34f9c2..2bbc8877 100644 --- a/clapper_src/appRemote.js +++ b/clapper_src/appRemote.js @@ -1,6 +1,7 @@ const { GObject } = imports.gi; const { AppBase } = imports.clapper_src.appBase; const { HeaderBarBase } = imports.clapper_src.headerbarBase; +const { WidgetRemote } = imports.clapper_src.widgetRemote; var AppRemote = GObject.registerClass( class ClapperAppRemote extends AppBase @@ -9,6 +10,9 @@ class ClapperAppRemote extends AppBase { super.vfunc_startup(); + let clapperWidget = new WidgetRemote(); + this.active_window.set_child(clapperWidget); + let headerBar = new HeaderBarBase(this.active_window); this.active_window.set_titlebar(headerBar); diff --git a/clapper_src/playerRemote.js b/clapper_src/playerRemote.js new file mode 100644 index 00000000..6a5b4c4d --- /dev/null +++ b/clapper_src/playerRemote.js @@ -0,0 +1,24 @@ +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 +{ + _init() + { + super._init(); + + this.webclient = new WebClient(settings.get_int('webserver-port')); + } + + set_playlist(playlist) + { + this.webclient.sendMessage({ + action: 'set_playlist', + value: playlist + }); + } +}); diff --git a/clapper_src/webClient.js b/clapper_src/webClient.js new file mode 100644 index 00000000..a1397f45 --- /dev/null +++ b/clapper_src/webClient.js @@ -0,0 +1,82 @@ +const { Soup, GObject } = imports.gi; +const Debug = imports.clapper_src.debug; +const WebHelpers = imports.clapper_src.webHelpers; + +let { debug } = Debug; + +var WebClient = GObject.registerClass( +class ClapperWebClient extends Soup.Session +{ + _init(port) + { + super._init({ + timeout: 3, + use_thread_context: true, + }); + + this.wsConn = null; + + this.connectWebsocket(port); + } + + connectWebsocket(port) + { + if(this.wsConn) + return; + + let 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'); + } + + sendMessage(data) + { + if( + !this.wsConn + || this.wsConn.state !== Soup.WebsocketState.OPEN + ) + return; + + this.wsConn.send_text(JSON.stringify(data)); + } + + passMsgData(action, value) + { + } + + _onWsConnect(session, result) + { + let connection = null; + + try { + connection = this.websocket_connect_finish(result); + } + catch(err) { + debug(err); + } + + if(!connection) + return; + + connection.connect('message', this._onWsMessage.bind(this)); + connection.connect('closed', this._onWsClosed.bind(this)); + + this.wsConn = connection; + + debug('successfully connected WebSocket'); + } + + _onWsMessage(connection, dataType, bytes) + { + const [success, parsedMsg] = WebHelpers.parseData(dataType, bytes); + + if(success) + this.passMsgData(parsedMsg.action, parsedMsg.value); + } + + _onWsClosed(connection) + { + debug('closed WebSocket connection'); + } +}); diff --git a/clapper_src/widgetRemote.js b/clapper_src/widgetRemote.js new file mode 100644 index 00000000..79a95961 --- /dev/null +++ b/clapper_src/widgetRemote.js @@ -0,0 +1,13 @@ +const { GObject, Gtk } = imports.gi; +const { PlayerRemote } = imports.clapper_src.playerRemote; + +var WidgetRemote = GObject.registerClass( +class ClapperWidgetRemote extends Gtk.Grid +{ + _init(opts) + { + super._init(); + + this.player = new PlayerRemote(); + } +});