websocket Laravel Echo + Laravel Passport在私人/在线网络套接字通道中授权

irtuqstp  于 2023-01-13  发布在  其他
关注(0)|答案(3)|浏览(155)

我正在尝试使用Laravel Echo连接到一个presence wesocket频道。我还在使用Laravel Passport。我学习了很多教程,但我得到了403个错误代码,302个代码等等。我在本地托管我的WebSocket服务器,使用laravel-websockets库来处理连接。Vue是我的前端库。
我是这么试的:

  • 编辑BroadcastServiceProvider的 Boot 函数。
public function boot() {
            Broadcast::routes(["prefix" => "api", "middleware" => "auth:api"]);
            require base_path('routes/channels.php');
        }
  • 授权令牌添加到回显标头,并在 bootstrap.js 中添加authEndpoint属性
window.Echo = new Echo({
 broadcaster: 'pusher',
 authEndpoint: '/broadcasting/auth',
 key: process.env.MIX_PUSHER_APP_KEY,
 auth:{
     headers:{
         'Authorization': 'Bearer ' + auth_token,
     }
 },
 cluster: process.env.MIX_PUSHER_APP_CLUSTER,
 //encrypted: false
 wsHost: window.location.hostname,
 wsPort: 6001});
  • 尝试从Vue加入频道
mounted(){
Echo.join(`game.0`)
    .here((users) => {
        alert("In the channel!");
    })
    .joining((user) => {
        console.log("Someone entered");
    })
    .leaving((user) => {
        console.log(user.name);
    })
    .listen('GameEvent', (e) => {
        console.log("Hey")
    });}

*路径/通道.php文件:

Route::prefix('/user')->group(function(){
Route::post('/login','api\v1\LoginController@Login');
Route::middleware('auth:api')->get('/all','api\v1\user\UserController@index');});

*事件文件

class GameEvent implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $gameEvent;
    public $gameId;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($gameEvent,$gameId)
    {
        //
        $this->gameEvent = $gameEvent;
        $this->gameId = $gameId;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        //return new PrivateChannel('channel-name');
        return new PresenceChannel('game.0');
    }

    public function broadcastWith(){
        return $this->gameEvent;
    }
}

此时,如果我尝试连接到该频道,broadcasting/auth 请求会得到一个302代码(已找到)。我在另一个帖子中找到了答案,该帖子声称可以通过将此添加到routes/web.php文件中来解决此问题。

Route::post('/broadcasting/auth', function(Request $request){
$pusher = new Pusher\Pusher(
    env('PUSHER_APP_KEY'),
    env('PUSHER_APP_SECRET'),
    env('PUSHER_APP_ID'),
    array(
        'cluster' => env('PUSHER_APP_CLUSTER'),
        'useTLS' => false,
        'host' => '127.0.0.1',
        'port' => 6001,
        'scheme' => 'http',
    )
);

IT WORK,* broadcasting/auth* 请求给出了一个200代码,我得到的响应如下:{"auth":"ABCFEDG:0bfff7db6506158755b012a47873799849d4e6b331f68da4bedd1caea04ad657"}

但是here, joining, leaving功能没有触发。

Echo.join('game.0')
    .here((users) => {
        alert("In the channel!");
    })

我该怎么做才能让它工作?
网络巡查员截图:

06odsfpq

06odsfpq1#

你是否在channel.php中添加了auth route?请检查一下你应该根据频道名称来设置

Route::channel('game.'.$id,function($user,$id){
return .....
})
nnvyjq4y

nnvyjq4y2#

那里没有频道。你错过广播频道了吗?

twh00eeo

twh00eeo3#

尝试按如下方式更改authEndpoint

authEndpoint: window.location.hostname+'/api/broadcasting/auth',

并且不要忘记更新您的广播服务提供商

Broadcast::routes([
    'prefix' => 'api',
    'middleware' => 'auth:api'
]);

require base_path('routes/channels.php');

相关问题