我正在使用 Pusher 实现 Soketi 解决方案(我没有使用 Echo)。我可以向专用频道发送通知,但是,我无法使用
channels.php
验证用户是否有权访问频道/端点。似乎channels.php
永远不会被触发。
routes/channels.php
Broadcast::channel('test.{id}', function ($user, $id)
{
\Log::debug('testing this endpoint'); // Never executed
return false; // Also never executed, doesn't matter if true or false
});
config/app.php
'providers' => ServiceProvider::defaultProviders()->merge([
\hisorange\BrowserDetect\ServiceProvider::class,
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\BroadcastServiceProvider::class, // <--- uncommented this line
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
])->toArray(),
应用程序/providers/BroadcastServiceProvider.php
class BroadcastServiceProvider extends ServiceProvider
{
public function boot(): void
{
Broadcast::routes();
require base_path('routes/channels.php');
}
}
TestEvent.php
class TestEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(public string $text)
{
//
}
public function broadcastOn(): array
{
return [
new PrivateChannel('test.3')
];
}
public function broadcastAs(): string
{
return 'message';
}
}
Pusher JS 配置
const pusher = new Pusher('app-key',
{
wsHost: '127.0.0.1',
wsPort: 6001,
forceTLS: false,
encrypted: true,
disableStats: true,
enabledTransports: ['ws', 'wss'],
authEndpoint: 'api/auth',
auth:
{
headers:
{
'Authorization': `Bearer ${token}`,
},
}
});
const channel = pusher.subscribe('private-test.3');
channel.bind('message', function(message)
{
console.log(message);
});
测试事件:
broadcast(new TestEvent('Hello World!'));
发生了什么:
broadcast/channel/test.{id}
下验证,这意味着 channels.php
永远不会被触发我错过了什么?
推杆日志
Pusher : ["State changed","initialized -> connecting"]
Pusher: ["Connecting",{"transport":"ws","url":"ws://127.0.0.1:6001/app/app-key?protocol=7&client=js&version=8.3.0&flash=false"}]
Pusher: ["State changed","connecting -> connected with new socket ID 8035059900.2497835364"]
Pusher: ["Event sent",{"event":"pusher:subscribe","data":{"auth":"app-key:8623bcc00ba6c28dea66645316bd2eb2943aa5c8115f1cac7cede17e4994809a","channel":"private-test.3"}}]
Pusher: ["Event recd",{"event":"pusher_internal:subscription_succeeded","channel":"private-test.3"}]
Pusher: ["No callbacks on private-test.3 for pusher:subscription_succeeded"]
Pusher: ["Event recd",{"event":"message","channel":"private-test.3","data":{"text":"Hello World!"}}]
my-js.js {text: 'Hello World!'}
解决了。
看来我被迫使用端点
api/broadcasting/auth
才能调用/执行channels.php
。
在 Pusher JS 配置上,我所要做的就是更改这一行:
authEndpoint: 'api/auth',
至:
authEndpoint: 'api/broadcasting/auth',