Allow disabling of websocket updates; improve NP time counter stability.

This commit is contained in:
Buster Neece 2019-07-31 13:47:12 -05:00
parent be7459c57d
commit bc1c2cb871
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
13 changed files with 85 additions and 65 deletions

View File

@ -69,7 +69,19 @@ return [
0 => __('Indefinitely'),
],
'default' => \App\Entity\SongHistory::DEFAULT_DAYS_TO_KEEP,
'form_group_class' => 'col-sm-12',
'form_group_class' => 'col-sm-6',
]
],
Entity\Settings::NOWPLAYING_USE_WEBSOCKETS => [
'toggle',
[
'label' => __('Use WebSockets for Now Playing Updates'),
'description' => __('Enables or disables the use of the newer and faster WebSocket-based system for receiving live updates on public players. You may need to disable this if you encounter problems with it.'),
'selected_text' => __('Yes'),
'deselected_text' => __('No'),
'default' => true,
'form_group_class' => 'col-md-6',
]
],

View File

@ -1,6 +1,7 @@
<?php
namespace App;
use App\Service\NChan;
use Azura\Settings;
use App\Entity;
use App\Http\Request;
@ -246,6 +247,18 @@ class Customization
return $title;
}
/**
* @return bool
*/
public function useWebSocketsForNowPlaying(): bool
{
if (!NChan::isSupported()) {
return false;
}
return (bool)$this->settings_repo->getSetting(Entity\Settings::NOWPLAYING_USE_WEBSOCKETS, true);
}
/**
* @param null $locale
*/

View File

@ -21,6 +21,8 @@ class Settings
public const ALWAYS_USE_SSL = 'always_use_ssl';
public const API_ACCESS_CONTROL = 'api_access_control';
public const NOWPLAYING_USE_WEBSOCKETS = 'nowplaying_use_websockets';
public const LISTENER_ANALYTICS = 'analytics';
public const CENTRAL_UPDATES = 'central_updates_channel';
public const SEND_ERROR_REPORTS = 'send_error_reports';

View File

@ -1,26 +1,11 @@
<?php
namespace App\Service;
use App\Message;
use App\Utilities;
use GuzzleHttp\Client;
/**
* Utility class for managing NChan, the nginx websocket/SSE/long-polling module.
*/
class NChan
{
/** @var Client */
protected $http_client;
/**
* @param Client $http_client
*/
public function __construct(Client $http_client)
{
$this->http_client = $http_client;
}
/**
* @return bool Whether NChan is expected to be running on this installation.
*/
@ -34,7 +19,45 @@ class NChan
return APP_DOCKER_REVISION >= 5;
}
$os_details = Utilities::getOperatingSystemDetails();
$os_details = self::getOperatingSystemDetails();
return 'bionic' === $os_details['VERSION_CODENAME'];
}
/**
* Pull operating system details.
* https://stackoverflow.com/questions/26862978/get-the-linux-distribution-name-in-php
*
* @return array
*/
public static function getOperatingSystemDetails(): array
{
$vars = [];
if (0 === stripos(PHP_OS, 'linux')) {
$files = glob('/etc/*-release');
foreach($files as $file)
{
$lines = array_filter(array_map(function($line) {
// split value from key
$parts = explode('=', $line);
// makes sure that "useless" lines are ignored (together with array_filter)
if (count($parts) !== 2) {
return false;
}
// remove quotes, if the value is quoted
$parts[1] = str_replace(array('"', "'"), '', $parts[1]);
return $parts;
}, file($file)));
foreach($lines as $line) {
$vars[$line[0]] = trim($line[1]);
}
}
}
return $vars;
}
}

View File

@ -211,44 +211,6 @@ class Utilities
return gethostbyname(gethostname()) ?? 'localhost';
}
/**
* Pull operating system details.
* https://stackoverflow.com/questions/26862978/get-the-linux-distribution-name-in-php
*
* @return array
*/
public static function getOperatingSystemDetails(): array
{
$vars = [];
if (0 === stripos(PHP_OS, 'linux')) {
$files = glob('/etc/*-release');
foreach($files as $file)
{
$lines = array_filter(array_map(function($line) {
// split value from key
$parts = explode('=', $line);
// makes sure that "useless" lines are ignored (together with array_filter)
if (count($parts) !== 2) {
return false;
}
// remove quotes, if the value is quoted
$parts[1] = str_replace(array('"', "'"), '', $parts[1]);
return $parts;
}, file($file)));
foreach($lines as $line) {
$vars[$line[0]] = trim($line[1]);
}
}
}
return $vars;
}
/**
* Flatten an array from format:
* [

View File

@ -20,7 +20,7 @@ $(function() {
});
<?php
if (\App\Service\NChan::isSupported()):
if ($customization->useWebSocketsForNowPlaying()):
?>
$(function() {
var sub = new NchanSubscriber('/api/live/nowplaying/<?=implode(',', $station_ids) ?>');

View File

@ -16,7 +16,7 @@ $assets
->addInlineJs($this->fetch('partials/radio_controls.js'), 95)
->addInlineJs($this->fetch('frontend/index/index.js', ['stations' => $stations]));
if (\App\Service\NChan::isSupported()) {
if ($customization->useWebSocketsForNowPlaying()) {
$assets->load('nchan');
}

View File

@ -25,7 +25,7 @@ $props = [
'show_album_art' => !$customization->hideAlbumArt(),
];
if (\App\Service\NChan::isSupported()) {
if ($customization->useWebSocketsForNowPlaying()) {
$props['use_nchan'] = true;
$props['now_playing_uri'] = '/api/live/nowplaying/'.urlencode($station->getShortName());
} else {

View File

@ -57,6 +57,13 @@ $(function() {
return formatTime(time_played) + ' / ' + formatTime(time_total);
}
},
methods: {
"setNowPlaying": function(np_new) {
np_new.now_playing.elapsed = this.np.now_playing.elapsed;
np_new.now_playing.elapsed = this.np.now_playing.elapsed;
this.np = np_new;
}
}
});
@ -109,13 +116,13 @@ $(function() {
});
<?php
if (\App\Service\NChan::isSupported()):
if ($customization->useWebSocketsForNowPlaying()):
?>
$(function() {
var sub = new NchanSubscriber('/api/live/nowplaying/<?=urlencode($station->getShortName()) ?>');
sub.on("message", function(message, message_metadata) {
nowPlaying.np = JSON.parse(message);
nowPlaying.setNowPlaying(JSON.parse(message));
});
sub.start();
});
@ -132,8 +139,7 @@ function loadNowPlaying() {
dataType: "json",
url: '<?=$router->fromHere('api:nowplaying:index') ?>',
success: function(row) {
nowPlaying.np = row;
nowPlaying.setNowPlaying(row);
nowPlayingTimeout = setTimeout(loadNowPlaying, 15000);
}
}).fail(function() {

View File

@ -18,7 +18,7 @@ $assets
->load('fancybox')
->addInlineJs($this->fetch('stations/profile/index.js', ['nowplaying' => $nowplaying]), 99);
if (\App\Service\NChan::isSupported()) {
if ($customization->useWebSocketsForNowPlaying()) {
$assets->load('nchan');
}
?>

View File

@ -44,7 +44,7 @@
"dist/lib/zxcvbn/zxcvbn.js": "dist/lib/zxcvbn/zxcvbn-9cf6916dc0.js",
"dist/light.css": "dist/light-33d5cc7dee.css",
"dist/material.js": "dist/material-c652fed16a.js",
"dist/radio_player.js": "dist/radio_player-c50382667e.js",
"dist/radio_player.js": "dist/radio_player-d5378c0524.js",
"dist/webcaster.js": "dist/webcaster-60f00ddbcb.js",
"dist/zxcvbn.js": "dist/zxcvbn-82c9dedeea.js"
}

File diff suppressed because one or more lines are too long

View File

@ -383,6 +383,8 @@ export default {
}
},
"handleNewNowPlaying": function(np_new) {
np_new.now_playing.elapsed = this.np.now_playing.elapsed;
np_new.now_playing.elapsed = this.np.now_playing.elapsed;
this.np = np_new;
// Set a "default" current stream if none exists.