[英]Laravel Websockets dashboard base path with sub folder not working
I am quite new to broadcasting/websockets and I am attempting to set up Laravel Websockets and pusher.我对广播/websockets 很陌生,我正在尝试设置 Laravel Websockets 和 pusher。
Using my subdomain.example.com
I can get subdomain.example.com/laravel-websockets
working and events get listed on the screen when I fire them using php artisan tinker and when I create an event on the page.使用我的
subdomain.example.com
我可以让subdomain.example.com/laravel-websockets
工作,并且当我使用 php artisan tinker 触发它们以及在页面上创建事件时,事件会在屏幕上列出。 However I need this work inside a path subdomain.example.com/api/laravel-websockets
但是我需要在路径
subdomain.example.com/api/laravel-websockets
这项工作
When I try the url with /api
although it loads the dashboard as expected it fails to connect and from looking in the console I can see it is ignoring the /api
in its requests, eg for Auth: http://subdomain.example.com/laravel-websockets/auth
instead of http://subdomain.example.com/api/laravel-websockets/auth
.当我尝试使用
/api
url 时,尽管它按预期加载了仪表板,但它无法连接,并且从控制台中查看我可以看到它忽略了其请求中的/api
,例如对于身份验证: http://subdomain.example.com/laravel-websockets/auth
而不是http://subdomain.example.com/api/laravel-websockets/auth
。 This applies to everything in the console.这适用于控制台中的所有内容。 Even when I edit the request in the console to add the
/api
it works.即使我在控制台中编辑请求以添加
/api
它也能工作。
Is there somewhere I can change the base path?有什么地方可以更改基本路径吗?
Here is my .env
file:这是我的
.env
文件:
APP_URL=http://subdomain.example.com/api/
LOG_CHANNEL=daily
BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
APP_NAME==test
PUSHER_APP_ID=1234
PUSHER_APP_KEY=1234
PUSHER_APP_SECRET=secret
PUSHER_APP_CLUSTER=mt1
Here is my config/websockets.php
pusher config这是我的
config/websockets.php
pusher 配置
'dashboard' => [
'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
],
'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'enable_client_messages' => true,
'enable_statistics' => true,
],
],
and my config/broadcasting.php
和我的
config/broadcasting.php
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http'
],
],
Edit: Adding my config to show how /api is working where /var/www/example
is the root folder of my Laravel application编辑:添加我的配置以显示 /api 是如何工作的,其中
/var/www/example
是我的 Laravel 应用程序的根文件夹
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName subdomain.example.com
ServerAlias subdomain.example.com
Alias /api /var/www/example/public
<Directory /var/www/example/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
DocumentRoot /var/www/example/client-side
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
in public/.htaccess I have the following:在 public/.htaccess 我有以下内容:
RewriteBase /api/
Edit编辑
I see that I can get this working if I add api/
to the javascript request paths right in the dashboard.blade.php
which makes me think the solution is have a version of that file running.我看到如果我将
api/
添加到dashboard.blade.php
的 javascript 请求路径,我可以让它工作,这让我认为解决方案是运行该文件的一个版本。 However I dont think this solves the overall issue.但是我不认为这解决了整体问题。 I would rather do this correctly.
我宁愿正确地做到这一点。 (see
authEndpoint
below) (请参阅下面的
authEndpoint
)
connect() {
this.pusher = new Pusher(this.app.key, {
wsHost: this.app.host === null ? window.location.hostname : this.app.host,
wsPort: this.port === null ? 6001 : this.port,
wssPort: this.port === null ? 6001 : this.port,
wsPath: this.app.path === null ? '' : this.app.path,
disableStats: true,
authEndpoint: '/api/{{ request()->path() }}/auth',
auth: {
headers: {
'X-CSRF-Token': "{{ csrf_token() }}",
'X-App-ID': this.app.id
}
},
enabledTransports: ['ws', 'flash']
});
我正在使用 Laravel 6.x,在dashboard.blade.php
我在第 121 和 165 行用Request::getRequestUri()
替换了request()->path()
,这提供了一个动态解决方案,但我仍然需要 orderride供应商dashboard.blade.php
文件...所以在github上打开了一个问题。
I will not mark this as the answer until someone has a better suggestion but I had to override dashboard.blade.php
and make my own one in resources/views/beyondcode/laravel-websockets/dashboard.blade.php
and add /api
where all calls are made.在有人有更好的建议之前,我不会将此标记为答案,但我必须覆盖
dashboard.blade.php
并在resources/views/beyondcode/laravel-websockets/dashboard.blade.php
创建我自己的一个并添加/api
where所有电话都进行了。
This actually fixed another issue I was having too in relation to not getting the real time graph.这实际上解决了我也遇到的另一个与无法获取实时图表有关的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.