1.优化websocket连接

2.修复游戏实施对局显示错误
This commit is contained in:
2026-04-24 16:53:00 +08:00
parent 203e478b65
commit d9b574676b
11 changed files with 67 additions and 145 deletions

View File

@@ -4,15 +4,37 @@ declare(strict_types=1);
namespace app\common\library\admin;
use Webman\Http\Request;
final class WebSocketConfigHelper
{
public static function wsUrl(): string
public static function wsUrl(?Request $request = null): string
{
$url = trim((string) env('H5_WEBSOCKET_URL', ''));
if ($url !== '') {
return $url;
}
return 'ws://127.0.0.1:3131';
if ($request !== null) {
$proto = strtolower((string) $request->header('x-forwarded-proto', ''));
if ($proto === '') {
$proto = strtolower((string) $request->header('x-forwarded-protocol', ''));
}
$isHttps = $proto === 'https'
|| strtolower((string) $request->header('x-forwarded-ssl', '')) === 'on'
|| strtolower((string) $request->header('x-scheme', '')) === 'https';
$scheme = $isHttps ? 'wss' : 'ws';
$host = trim((string) $request->header('host', ''));
if ($host === '') {
$host = trim((string) $request->header('x-forwarded-host', ''));
}
if ($host !== '') {
return $scheme . '://' . $host . '/ws/';
}
}
return 'ws://127.0.0.1:3131/ws/';
}
}