【发布时间】:2020-02-01 15:47:36
【问题描述】:
我对广播/websockets 很陌生,我正在尝试设置 Laravel Websockets 和推送器。
使用我的subdomain.example.com 我可以让subdomain.example.com/laravel-websockets 工作,当我使用 php artisan tinker 触发它们并在页面上创建事件时,事件会在屏幕上列出。但是我需要在路径 subdomain.example.com/api/laravel-websockets
当我尝试使用/api 的网址时,尽管它按预期加载仪表板,但它无法连接,并且从控制台中查看我可以看到它忽略了其请求中的/api,例如对于身份验证:http://subdomain.example.com/laravel-websockets/auth 而不是 http://subdomain.example.com/api/laravel-websockets/auth。这适用于控制台中的所有内容。即使我在控制台中编辑请求以添加 /api 它也有效。
有什么地方可以更改基本路径吗?
这是我的.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
这是我的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,
],
],
还有我的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'
],
],
编辑:添加我的配置以显示 /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
在 public/.htaccess 我有以下内容:
RewriteBase /api/
编辑
我发现如果我将api/ 添加到dashboard.blade.php 中的javascript 请求路径中,我可以得到这个工作,这让我认为解决方案是运行该文件的一个版本。但是我认为这不能解决整体问题。我宁愿正确地做到这一点。 (见下文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']
});
【问题讨论】:
-
您不能只是更改路径并期望它起作用。您还必须更改该路径的路由处理。请参阅 config/websockets.php 文件,其中包含用于处理 Web 套接字的路径。
-
我想你不是在谈论 config/websockets.php 中的路径?正如预期的那样,它就像“laravel-websockets”一样工作正常。如果我将其更改为 'api/laravel-websockets' 会破坏我的网址。并不是说我无法加载 url,它的基本路径仍然只使用域。感谢您的帮助,对不起,我是这个概念的新手!
-
我将编辑问题以添加我的虚拟主机配置