我正在使用,它对公共频道非常有用。但是,当我切换到专用频道时,事件会被触发,但我的客户端无法侦听。我发现通道授权回调没有被调用,我不知道为什么。我的活动:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class MessageSentEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn(): PrivateChannel
{
return new PrivateChannel("message.".$this->message->receiver);
}
}我的api.php频道授权广播线路
Route::post("/broadcasting/auth",function(){
\Illuminate\Support\Facades\Log::info("Route is reached"); //this is called
Broadcast::channel("message.{user_id}",function($user,$message){
\Illuminate\Support\Facades\Log::info("Authorizing channel"); //this is not called
return true;
});
})->middleware(["auth:sms"]);我的机敏的客户听着这件事:
const {value } = await Preferences.get({key:"auth_token"});
let echo = new Echo({
broadcaster: 'pusher',
key: 'pusher_key',
wsHost: environment.websocker_server_development,
wsPort: 6001,
wssPort:6001,
encrypted:true,
forceTLS: false,
bearerToken:value,
authEndpoint:"http://192.168.43.221:8000/api/broadcasting/auth",
auth:{
headers:{
"Authorization":`Bearer ${value}`,
'Content-Type': 'application/json'
}
}
});
echo.private(`message.${this.user.id}`).listen("MessageSentEvent",(e)=>{
console.log(e)
})同样,当一个公共频道被使用时,这个功能也是完美的,但是一个私有的通道却不能工作。我怀疑它不起作用,我怀疑这是因为广播::频道的回调是不叫的。为什么不叫呢?
发布于 2022-10-04 16:24:00
我找到了解决办法。首先是将channels.php文件用于路由:
Broadcast::channel("message.{id}",function(){
\Illuminate\Support\Facades\Log::info("Authorizing channel");
return true;
});最重要的是,我必须添加api中间件,以便广播身份验证器停止期望会话请求:
public function boot()
{
Broadcast::routes(["prefix"=>"api","middleware"=>["api","auth:sms"]]);
require base_path('routes/channels.php');
}https://stackoverflow.com/questions/73945403
复制相似问题