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 $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 ''; } }