fix: websocket获取链接失败

This commit is contained in:
kuaifan 2022-01-14 23:15:59 +08:00
parent 361484be95
commit b019d40009

View File

@ -725,7 +725,7 @@ class Base
public static function getHost($var = '') public static function getHost($var = '')
{ {
if (empty($var)) { if (empty($var)) {
$var = url("/"); $var = self::url();
} }
$arr = parse_url($var); $arr = parse_url($var);
return $arr['host']; return $arr['host'];
@ -756,11 +756,7 @@ class Base
) { ) {
return $str; return $str;
} else { } else {
$url = url($str); return self::url($str);
if (str_starts_with($url, "http://localhost/")) {
$url = self::localhostAndPort() . substr($url, 17);
}
return $url;
} }
} }
@ -777,16 +773,35 @@ class Base
} }
return $str; return $str;
} }
$str = Base::leftDelete($str, url('') . '/'); return Base::leftDelete($str, self::url() . '/');
return Base::leftDelete($str, self::localhostAndPort());
} }
/** /**
* 获取localhost和端口http://localhost:8888/ * 获取url
* @return string * @param $path
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\UrlGenerator|string
*/ */
public static function localhostAndPort() { public static function url($path = '')
return "http://localhost:" . env("APP_PORT", "80") . "/"; {
try {
$url = url($path);
if (str_starts_with($url, "http://localhost/")) {
$url = "http://localhost:" . env("APP_PORT", "80") . "/" . substr($url, 17);
}
} catch (\Throwable) {
$url = self::getSchemeAndHost() . "/" . $path;
}
return $url;
}
/**
* 获取主地址
* @return string http://127.0.0.1:8080
*/
public static function getSchemeAndHost()
{
$scheme = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
return $scheme.($_SERVER['HTTP_HOST'] ?? '');
} }
/** /**