我一直在尝试使用 Pusher 设置我的广播系统,并按照文档一步步操作。当我启动服务器时出现错误,
“未捕获的选项对象必须提供一个簇”
在我的控制台中。
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'host' => env('PUSHER_HOST', 'api-'.env('PUSHER_APP_CLUSTER', 'mt1').'.pusher.com') ?: 'api-'.env('PUSHER_APP_CLUSTER', 'mt1').'.pusher.com',
'port' => env('PUSHER_PORT', 443),
'scheme' => env('PUSHER_SCHEME', 'https'),
'encrypted' => true,
'useTLS' => env('PUSHER_SCHEME', 'https') === 'https',
],
'client_options' => [
// Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html
],
],
所以我检查了广播配置中的选项对象,发现我的配置没有集群选项,所以我手动添加了它,但仍然有这个错误。
.env
PUSHER_APP_ID=1529400
PUSHER_APP_KEY=521a8d3a78ab50e2c14d
PUSHER_APP_SECRET=ce93e12b5f74f8280624
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
e here
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'host' => env('PUSHER_HOST', 'api-'.env('PUSHER_APP_CLUSTER', 'mt1').'.pusher.com') ?: 'api-'.env('PUSHER_APP_CLUSTER', 'mt1').'.pusher.com',
'port' => env('PUSHER_PORT', 443),
'scheme' => env('PUSHER_SCHEME', 'https'),
'encrypted' => true,
'useTLS' => env('PUSHER_SCHEME', 'https') === 'https',
'cluster' => env('PUSHER_APP_CLUSTER'),
],
'client_options' => [
// Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html
],
],
我遇到了同样的错误,并通过将
cluster
添加到新 Echo 实例中传递的参数来解决它:
bootstrap.js:
已添加
cluster:import.meta.env.VITE_PUSHER_APP_CLUSTER,
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'pusher',
key: import.meta.env.VITE_PUSHER_APP_KEY,
wsHost: import.meta.env.VITE_PUSHER_HOST ?? `ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`,
wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
disableStats: true,
cluster:import.meta.env.VITE_PUSHER_APP_CLUSTER,//added this line
});
另外,请确保将其添加到选项数组中的
broadcasting.php
中:
'connections' => [
'pusher' => [
'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'
],
'client_options' => [
// Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html
],
],
...
编辑 我意识到在 8.0.0 版本中,集群选项是强制性的。另一个问题是,如果您使用 Laravel-WebSockets 包作为推送器的直接替代品,这将是一个问题,因为在生产中,推送器连接到您在选项中提供的集群。所以我所做的是卸载了 Pusher-js 8.0.0 版本并安装了不需要集群的 Pusher 7.6 版本:
npm i [email protected]
在 bootstrap.js 上执行此操作,对我来说效果很好:
window.Echo = new Echo({
broadcaster: 'pusher',
key: import.meta.env.VITE_ABLY_PUBLIC_KEY,
wsHost: 'realtime-pusher.ably.io',
wsPort: 443,
disableStats: true,
encrypted: true,
cluster: 'eu',
});
您可以在此处选择集群位置: https://pusher.com/docs/channels/miscellaneous/clusters/ 在那里,你放什么并不重要。我正在使用 ively.com 作为套接字
约书亚,
我遇到了同样的问题。
推送者现在将其强制执行:https://github.com/pusher/pusher-js/releases
我正在将我的代码回滚到以前版本的 Pusher,希望能起作用。