Add initial WebSocket client app

This commit is contained in:
Rafostar
2020-12-15 18:20:48 +01:00
parent 8564cc9617
commit 5231a1f225
4 changed files with 123 additions and 0 deletions

View File

@@ -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);

View File

@@ -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
});
}
});

82
clapper_src/webClient.js Normal file
View File

@@ -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');
}
});

View File

@@ -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();
}
});