chart fixed
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WebsocketBroadcastService
|
||||
{
|
||||
private const DEFAULT_TRIGGER_PATH = '/prc/api/trigger-update';
|
||||
|
||||
public function triggerBaseUrl(): ?string
|
||||
{
|
||||
$fromEnv = trim((string) (env('WSS_LINK') ?: env('wssLink', '')));
|
||||
if ($fromEnv !== '') {
|
||||
return rtrim($fromEnv, '/');
|
||||
}
|
||||
|
||||
$fromSetting = trim((string) app(ConfigService::class)->getString('wss_link', ''));
|
||||
if ($fromSetting !== '') {
|
||||
return rtrim($fromSetting, '/');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function triggerPath(): string
|
||||
{
|
||||
$path = trim((string) app(ConfigService::class)->getString('wss_trigger_path', ''));
|
||||
|
||||
return $path !== '' ? $path : self::DEFAULT_TRIGGER_PATH;
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
if (! app(ConfigService::class)->getBool('wss_enabled', true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->triggerBaseUrl() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
public function sendEvent(string $trigger, array $payload = []): void
|
||||
{
|
||||
if (! $this->isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$baseUrl = $this->triggerBaseUrl();
|
||||
if ($baseUrl === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$body = array_merge($payload, ['trigger' => $trigger]);
|
||||
$host = $this->resolveHost();
|
||||
if ($host !== '') {
|
||||
$body['host'] = $host;
|
||||
}
|
||||
|
||||
$url = $baseUrl.$this->triggerPath();
|
||||
|
||||
try {
|
||||
Http::timeout(3)
|
||||
->withHeaders(['Content-Type' => 'application/json'])
|
||||
->withOptions([
|
||||
'curl' => [
|
||||
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||
],
|
||||
])
|
||||
->post($url, $body);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('Websocket broadcast failed', [
|
||||
'trigger' => $trigger,
|
||||
'url' => $url,
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveHost(): string
|
||||
{
|
||||
$config = app(ConfigService::class);
|
||||
$nameEn = trim((string) $config->getString('name_en', ''));
|
||||
if ($nameEn !== '') {
|
||||
return $nameEn;
|
||||
}
|
||||
|
||||
if (! app()->runningInConsole() && request()) {
|
||||
return trim((string) request()->getHost());
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user