问题描述
我正在使用Laravel Event Broadcast和Pusher在我的API和Web上同时使用websocket.如果我单独尝试,它们都可以正常工作.我的意思是:
I am using Laravel Event Broadcast and Pusher to utilize websockets both on my API and Web. If I try them individually, both work fine. What I mean is:
Broadcast::routes(['middleware' => 'web']); // this works for my Laravel website
Broadcast::routes(['middleware' => 'auth:api']); // this works for my api
但是,如果我想像这样同时使用两者:
However, if I want to use both at the same time like this:
Broadcast::routes(['middleware' => ['auth:api', 'web']]); // doesn't work
...两者均崩溃,我怀疑这是假设我正在尝试同时启用两个auth:api && web
中间件.
... it crashes for both, which I suspect that it is assuming I am trying to enable for both auth:api && web
middlewares.
是否有一种方法可以对此使用OR类型的语句(auth::api || 'web')
?如果我想同时使用两者,并且如果它通过了一个中间件,它将绕过中间件怎么办?
Is there a way to use an OR kind of statement for this (auth::api || 'web')
? What if I want to use both at the same time and if it passes one middleware, it bypasses the middleware.
或者是否有一种方法可以为两者组合并创建混合的中间件(本质上将检查api或web)?所以我可以使用类似这样的东西:
Or is there a way to combine and creating a mixed middleware for both (which will essentially check for either api or web)? So I can use something like this maybe:
Broadcast::routes(['middleware' => 'broadcast']); // or auth:broadcast
更新:
据我了解,如果创建一个名为broadcast
的新中间件,则可以:
As far as I understand, if I create a new Middleware called broadcast
, I can do:
class BroadcastMiddleware() {
public function handle() {
$web = Auth::guard('web')->user();
if ($web) {
return response()->json($web);
}
$api = Auth::guard('api')->user();
if ($api) {
return response()->json($api);
}
return response()->json('Unauthorized.', 500);
}
}
但是如何更改/broadcasting/auth
路线?如果我尝试这样做:
But then how do I change /broadcasting/auth
route? If I try this:
Route::post('/realtime/auth', function(){
return true;
})->middleware('broadcast');
这将返回用户对象信息,但是,它应该返回如下内容:auth:"374f7ff42b7da877aa35:c29addedec281b418331a61dc3cfc74da8b108222565fa4924a8..."
This returns the user object info, however instead, it should return something like: auth:"374f7ff42b7da877aa35:c29addedec281b418331a61dc3cfc74da8b108222565fa4924a8..."
推荐答案
我终于弄清楚了该怎么做.
I finally figured out how to do it.
我不确定这是否是实现此目标的最佳方法,我非常感谢您所做的任何改进.
我如何实现的是为"web"创建了一个新的中间件,而将另一个保持不变.步骤如下.
How I achieved is created a new middleware for 'web' and left the other one as it it. Here are the steps.
1)在"BroadcastServiceProvider"中,仅将auth:api
保护保留为Broadcast::routes(['middleware' => 'auth:api']);
.
1) In 'BroadcastServiceProvider', left only auth:api
guard for Broadcast::routes(['middleware' => 'auth:api']);
.
这样,用于验证广播的Laravel的auth:api
方法可以按预期工作.
This way, Laravel's auth:api
method for authenticating broadcasting works as expected.
2)创建了一个名为广播"的中间件,并将其映射到Kernel.php中,如下所示:
2) Created a middleware called "Broadcast" and mapped it in Kernel.php like so:
'broadcast' => \App\Http\Middleware\Broadcast::class
和Broadcast.php
中间件如下:
public function handle($request, Closure $next)
{
$web = Auth::guard('web')->user();
if ($web) {
return response()->json(\Illuminate\Support\Facades\Broadcast::auth($request));
}
return response()->json('Unauthorized.', 500);
}
3)在我的路线> web.php
3) Created a unique route other than Laravel's /broadcasting/auth in my routes>web.php
Route::post('/guard/broadcast/auth', function(\Illuminate\Support\Facades\Request $req){
return true;
})->middleware('broadcast');
4)然后仅在刀片上使用,就像这样:
4) And then only on my blade, I use it like so:
<script>
let pusher = new Pusher("{{ env('PUSHER_APP_KEY') }}", {
cluster: 'us2',
encrypted: true,
auth: {
headers: {
'X-CSRF-TOKEN': "{{ csrf_token() }}"
}
},
authEndpoint: '{{ env('APP_URL') }}' + '/guard/broadcast/auth',
});
let channel = pusher.subscribe('private-channel.{{ Auth::user()->id }}');
channel.bind('my-event', addMessage);
function addMessage(data) {
console.log(data);
}
</script>
这篇关于Laravel Broadcast-结合多个中间件(网络,auth:api)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!