We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在 config/app.php 中我们能看到 Illuminate\Broadcasting\BroadcastServiceProvider 这个服务提供者被注册:
config/app.php
Illuminate\Broadcasting\BroadcastServiceProvider
laravel/config/app.php
Line 128 in 72bccf5
boot
register
BroadcastServiceProvider
laravel/app/Providers/BroadcastServiceProvider.php
Lines 10 to 20 in 72bccf5
第17行调用到了 Illuminate\Support\Facades\Broadcast::routes() 假面方法。
Illuminate\Support\Facades\Broadcast::routes()
Line 17 in 72bccf5
顾名思义,就是注册路由的。而这个方法,最终是穿透到了 Illuminate\Broadcasting\BroadcastManager::routes()
Illuminate\Broadcasting\BroadcastManager::routes()
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php
Lines 53 to 73 in 72bccf5
第19行是为了在 config/app.php 卸载 BroadcastServiceProvider 时,直观的让 routes/channels.php 失效的做法,所以就不放在别处
routes/channels.php
Line 19 in 72bccf5
第61~63行是如果缓存过路由,则跳出。
第62~第72行相当于与执行了
Route::group(['middleware' => ['web']], function () { Route::match(['get', 'post'], '/broadcasting/auth', '\\'.BroadcastController::class.'@authenticate'); });
方法调用到的 controller action 其实是
controller action
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastController.php
Lines 11 to 20 in 72bccf5
这个假面方法 Broadcast::auth() 比较特殊,是根据 config('broadcasting.default') 分发到 Illuminate/Broadcasting/Broadcasters 具体的类中:
Broadcast::auth()
config('broadcasting.default')
我们以 redis 的配置为例 (laravel-echo-server) 。
redis
laravel-echo-server
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php
Lines 39 to 61 in 72bccf5
第一步,如果 $request->channel_name 不以 private- 或者 presence- 开头,或者没有提供用户授权导致请求取不到用户,抛出 Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException 异常
$request->channel_name
private-
presence-
Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
第二步,将 $request->channel_name 中的 private- 或者 presence- 还原移除。
第三步,调用 Broadcaster:: verifyUserCanAccessChannel()
Broadcaster:: verifyUserCanAccessChannel()
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php
Lines 45 to 71 in 72bccf5
将 routes/channels.php 中定义的 channel_name 匹配上的通道的鉴权回调执行,拿到返回值。如果 if ($result = $handler($request->user(), ...$parameters)) 返回成功的 RedisBroadcaster::validAuthenticationResponse(),否则抛出 Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException 异常
channel_name
if ($result = $handler($request->user(), ...$parameters))
RedisBroadcaster::validAuthenticationResponse()
而成功的响应 RedisBroadcaster::validAuthenticationResponse() 逻辑为返回 user_id 和 user_info 给 websocket 客户端:
user_id
user_info
Lines 63 to 80 in 72bccf5
至此,鉴权部分完成。
broadcast() 方法定义于
broadcast()
laravel/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
Lines 216 to 219 in 72bccf5
此方法最终走到了
Lines 92 to 101 in 72bccf5
PendingBroadcast 类比较特殊,有个 __destruct 解构方法:
PendingBroadcast
__destruct
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/PendingBroadcast.php
Lines 50 to 58 in 72bccf5
走到
laravel/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php
Lines 185 to 229 in 72bccf5
调用了
Lines 272 to 281 in 72bccf5
还是回到了 BroadcastManager
BroadcastManager
Lines 103 to 130 in 72bccf5
第127~129行,是将广播事件 BroadcastEvent 入队。
BroadcastEvent
根据我们对队列的了解,BroadcastEvent 应该有 handle() 方法
handle()
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php
Lines 35 to 50 in 72bccf5
第46~49行,是执行广播,调用了 RedisBroadcaster::broadcast()
RedisBroadcaster::broadcast()
Lines 82 to 103 in 72bccf5
消息进入 redis。
// TODO: laravel-echo-server 部分。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
鉴权
在
config/app.php
中我们能看到Illuminate\Broadcasting\BroadcastServiceProvider
这个服务提供者被注册:laravel/config/app.php
Line 128 in 72bccf5
在前面对服务提供者的讲解中,我们得出了一个服务提供者肯定会包含
boot
或register
方法之一,这BroadcastServiceProvider
就是一个包含而且只包含了boot
方法的服务提供者:laravel/app/Providers/BroadcastServiceProvider.php
Lines 10 to 20 in 72bccf5
第17行调用到了
Illuminate\Support\Facades\Broadcast::routes()
假面方法。laravel/app/Providers/BroadcastServiceProvider.php
Line 17 in 72bccf5
顾名思义,就是注册路由的。而这个方法,最终是穿透到了
Illuminate\Broadcasting\BroadcastManager::routes()
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php
Lines 53 to 73 in 72bccf5
第19行是为了在
config/app.php
卸载BroadcastServiceProvider
时,直观的让routes/channels.php
失效的做法,所以就不放在别处laravel/app/Providers/BroadcastServiceProvider.php
Line 19 in 72bccf5
第61~63行是如果缓存过路由,则跳出。
第62~第72行相当于与执行了
方法调用到的
controller action
其实是laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastController.php
Lines 11 to 20 in 72bccf5
这个假面方法
Broadcast::auth()
比较特殊,是根据config('broadcasting.default')
分发到 Illuminate/Broadcasting/Broadcasters 具体的类中:我们以
redis
的配置为例 (laravel-echo-server
) 。laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php
Lines 39 to 61 in 72bccf5
第一步,如果
$request->channel_name
不以private-
或者presence-
开头,或者没有提供用户授权导致请求取不到用户,抛出Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
异常第二步,将
$request->channel_name
中的private-
或者presence-
还原移除。第三步,调用
Broadcaster:: verifyUserCanAccessChannel()
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php
Lines 45 to 71 in 72bccf5
将
routes/channels.php
中定义的channel_name
匹配上的通道的鉴权回调执行,拿到返回值。如果if ($result = $handler($request->user(), ...$parameters))
返回成功的RedisBroadcaster::validAuthenticationResponse()
,否则抛出Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
异常而成功的响应
RedisBroadcaster::validAuthenticationResponse()
逻辑为返回user_id
和user_info
给 websocket 客户端:laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php
Lines 63 to 80 in 72bccf5
至此,鉴权部分完成。
推送
broadcast()
方法定义于laravel/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
Lines 216 to 219 in 72bccf5
此方法最终走到了
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php
Lines 92 to 101 in 72bccf5
PendingBroadcast
类比较特殊,有个__destruct
解构方法:laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/PendingBroadcast.php
Lines 50 to 58 in 72bccf5
走到
laravel/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php
Lines 185 to 229 in 72bccf5
调用了
laravel/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php
Lines 272 to 281 in 72bccf5
还是回到了
BroadcastManager
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php
Lines 103 to 130 in 72bccf5
根据前文的
config('broadcasting.default')
配置,第127~129行,是将广播事件
BroadcastEvent
入队。根据我们对队列的了解,
BroadcastEvent
应该有handle()
方法laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php
Lines 35 to 50 in 72bccf5
第46~49行,是执行广播,调用了
RedisBroadcaster::broadcast()
laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php
Lines 82 to 103 in 72bccf5
消息进入 redis。
// TODO: laravel-echo-server 部分。
The text was updated successfully, but these errors were encountered: