今天,我们将在Laravel Web框架中探索广播的概念。当服务器端发生某些事情时,它允许您将通知发送到客户端。在本文中,我们将使用第三方Pusher库将通知发送到客户端。
>
如果您曾经想在Laravel中的服务器上发生某些事情时,您正在寻找广播功能的某些事情时将通知从服务器发送到客户端。现在,当用户A向用户B发送消息时,您需要实时通知用户B。 You may display a popup or an alert box that informs user B about the new message!It's the perfect use-case to walk through the concept of broadcasting in Laravel, and that's what we'll implement in this article.If you are wondering how the server could send notifications to the client, it's using sockets under the hood to accomplish it.让我们了解插座的基本流程,然后再深入研究实际实施。 首先,您需要一台支持Web-Sockets协议的服务器,允许客户端建立Web套接字连接。- 您可以实现自己的服务器或使用自己的服务器或使用第三方服务。我们将更喜欢本文中的后者。
- >客户在成功连接时启动了与Web套接字服务器的Web套接字连接,并在连接成功时接收唯一的标识符。
- > 连接成功,客户将订阅某些频道,它将订阅它想要接收事件的某些频道。服务器端,当发生特定事件时,我们通过提供频道名称和事件名称来告知Web-Socket服务器。
- ,最后,Web-Socket Server将该事件广播到该特定频道上注册的客户端。
- >
- >在单个go中看起来是否太多,请不要担心。当我们浏览本文时,您将掌握它。
- 广播配置文件
- > 接下来,让我们查看
。
<?php <br><br>return [<br><br> /*<br> |--------------------------------------------------------------------------<br> | Default Broadcaster<br> |--------------------------------------------------------------------------<br> |<br> | This option controls the default broadcaster that will be used by the<br> | framework when an event needs to be broadcast. You may set this to<br> | any of the connections defined in the "connections" array below.<br> |<br> | Supported: "pusher", "redis", "log", "null"<br> |<br> */<br><br> 'default' => env('BROADCAST_DRIVER', 'null'),<br><br> /*<br> |--------------------------------------------------------------------------<br> | Broadcast Connections<br> |--------------------------------------------------------------------------<br> |<br> | Here you may define all of the broadcast connections that will be used<br> | to broadcast events to other systems or over websockets. Samples of<br> | each available type of connection are provided inside this array.<br> |<br> */<br><br> 'connections' => [<br><br> 'pusher' => [<br> 'driver' => 'pusher',<br> 'key' => env('PUSHER_APP_KEY'),<br> 'secret' => env('PUSHER_APP_SECRET'),<br> 'app_id' => env('PUSHER_APP_ID'),<br> 'options' => [<br> 'cluster' => env('PUSHER_APP_CLUSTER'),<br> 'useTLS' => true,<br> ],<br> ],<br><br> 'redis' => [<br> 'driver' => 'redis',<br> 'connection' => 'default',<br> ],<br><br> 'log' => [<br> 'driver' => 'log',<br> ],<br><br> 'null' => [<br> 'driver' => 'null',<br> ],<br><br> ],<br><br>];<br>的默认广播配置文件,默认情况下,请访问多个core new eyan new new new nect of there norker of there corect of。使用日志适配器。当然,如果您将推动器
适配器用作我们的默认广播驱动程序。 So let's change the migration file database/migrations/XXXX_XX_XX_XXXXXX_create_messages_table.php before running the migrate command.
...<br>...<br>BROADCAST_DRIVER=pusher<br><br>PUSHER_APP_ID={YOUR_APP_ID}<br>PUSHER_APP_KEY={YOUR_APP_KEY}<br>PUSHER_APP_SECRET={YOUR_APP_SECRET}<br>PUSHER_APP_CLUSTER={YOUR_APP_CLUSTER}<br>...<br>...<br>
Now, let's run the messages table in the database. 如果事件是正常事件,Laravel称之为相关的侦听器类。另一方面,如果事件是广播类型的,Laravel将该事件发送到在> config/broadcasting.php > >如果一切顺利,如果一切顺利,则应将Web-Socket连接与推动器Web-Socket Server和IT列表的 user.{USER_ID}<?php <br><br>return [<br><br> /*<br> |--------------------------------------------------------------------------<br> | Default Broadcaster<br> |--------------------------------------------------------------------------<br> |<br> | This option controls the default broadcaster that will be used by the<br> | framework when an event needs to be broadcast. You may set this to<br> | any of the connections defined in the "connections" array below.<br> |<br> | Supported: "pusher", "redis", "log", "null"<br> |<br> */<br><br> 'default' => env('BROADCAST_DRIVER', 'null'),<br><br> /*<br> |--------------------------------------------------------------------------<br> | Broadcast Connections<br> |--------------------------------------------------------------------------<br> |<br> | Here you may define all of the broadcast connections that will be used<br> | to broadcast events to other systems or over websockets. Samples of<br> | each available type of connection are provided inside this array.<br> |<br> */<br><br> 'connections' => [<br><br> 'pusher' => [<br> 'driver' => 'pusher',<br> 'key' => env('PUSHER_APP_KEY'),<br> 'secret' => env('PUSHER_APP_SECRET'),<br> 'app_id' => env('PUSHER_APP_ID'),<br> 'options' => [<br> 'cluster' => env('PUSHER_APP_CLUSTER'),<br> 'useTLS' => true,<br> ],<br> ],<br><br> 'redis' => [<br> 'driver' => 'redis',<br> 'connection' => 'default',<br> ],<br><br> 'log' => [<br> 'driver' => 'log',<br> ],<br><br> 'null' => [<br> 'driver' => 'null',<br> ],<br><br> ],<br><br>];<br>
>创建事件类
每当您想在Laravel提出自定义事件时,都应为该事件创建类。基于事件的类型,Laravel做出了相应的反应并采取必要的操作。private
移动进一步,我们使用 private <code>user.{USER_ID}
Echo的方法来订阅私有渠道用户。{user_id} <code>Echo
。正如我们前面讨论的那样,客户必须在订阅私人渠道之前对自己进行身份验证。因此, echo<code>user.{USER_ID}
对象通过使用必要的参数在后台发送XHR来执行必要的身份验证。最后,Laravel试图找到用户。{user_id}<strong>路由,它应该与我们在<ancoutes>文件中定义的路由相匹配。</ancoutes></strong>
newMessageNotification <antemification>事件,因此我们使用了<code> listan> listan> echo> echo <y>的方法来实现它。为了使事情变得简单,我们只会提醒我们从Pusher Server收到的消息。<p>><code>NewMessageNotification
,这就是从Web-Sockockets服务器接收事件的设置。接下来,我们将在控制器文件中浏览 send<code>listen
的方法,该方法提出了广播事件。Echo
>
send <code> send<p>> <code>send
方法的代码。>send
send<pre class="brush:php;toolbar:false">...<br>...<br>BROADCAST_DRIVER=pusher<br><br>PUSHER_APP_ID={YOUR_APP_ID}<br>PUSHER_APP_KEY={YOUR_APP_KEY}<br>PUSHER_APP_SECRET={YOUR_APP_SECRET}<br>PUSHER_APP_CLUSTER={YOUR_APP_CLUSTER}<br>...<br>...<br></pre>方法中模仿该行为。<p><code>send
接下来,我们使用 event <code> event
助手函数来提高 newMessageNotification <anementification>事件。由于<code> newMessAgeNotification <ante> event属于<code> shopsbroadcastNow<code>event
type type type type type NewMessageNotification
文件中加载默认的广播配置。最后,它将 newMessAgeNotification<code>ShouldBroadcastNow
事件广播到用户上的已配置的Web-Socket服务器。在我们的情况下,该事件将广播到<ancy>频道上的Pusher Web-Socket服务器。 If the ID of the recipient user is <p>, the event will be broadcast over the <code>user.{USER_ID}
channel.1
user.1
As we discussed earlier, we already have a setup that listens to events on this channel, so it should be able to receive this event, and the alert box is displayed to the user!
How to Test Our Setup
Let's go ahead and walk through how you are supposed to test the use-case that到目前为止,我们已经构建了。
在您的浏览器中打开URL https:// your-laravel-site-domain/message/index。如果您尚未登录,您将被重定向到登录屏幕。登录后,您应该看到我们之前定义的广播视图 - 尚无幻想。当我们启用了按Pusher客户端库提供的
>设置时,它将在浏览器控制台中记录所有内容以进行调试。让我们看看当您访问http:// your-laravel-site-domain/message/index Page时,它将记录到控制台的内容。
>Pusher.logToConsole
>
<?php <br><br>return [<br><br> /*<br> |--------------------------------------------------------------------------<br> | Default Broadcaster<br> |--------------------------------------------------------------------------<br> |<br> | This option controls the default broadcaster that will be used by the<br> | framework when an event needs to be broadcast. You may set this to<br> | any of the connections defined in the "connections" array below.<br> |<br> | Supported: "pusher", "redis", "log", "null"<br> |<br> */<br><br> 'default' => env('BROADCAST_DRIVER', 'null'),<br><br> /*<br> |--------------------------------------------------------------------------<br> | Broadcast Connections<br> |--------------------------------------------------------------------------<br> |<br> | Here you may define all of the broadcast connections that will be used<br> | to broadcast events to other systems or over websockets. Samples of<br> | each available type of connection are provided inside this array.<br> |<br> */<br><br> 'connections' => [<br><br> 'pusher' => [<br> 'driver' => 'pusher',<br> 'key' => env('PUSHER_APP_KEY'),<br> 'secret' => env('PUSHER_APP_SECRET'),<br> 'app_id' => env('PUSHER_APP_ID'),<br> 'options' => [<br> 'cluster' => env('PUSHER_APP_CLUSTER'),<br> 'useTLS' => true,<br> ],<br> ],<br><br> 'redis' => [<br> 'driver' => 'redis',<br> 'connection' => 'default',<br> ],<br><br> 'log' => [<br> 'driver' => 'log',<br> ],<br><br> 'null' => [<br> 'driver' => 'null',<br> ],<br><br> ],<br><br>];<br>>方法的过程中保持此页面打开。
send
接下来,让我们打开http:// your-laravel-site-domain/message/message/message/message/send url在另一个选项卡或其他浏览器中发送url。如果您要使用其他浏览器,则需要登录才能访问该页面。发生了。
,它告诉您,您刚刚从
>频道上的Pusher Web-Socket Server收到了事件。转到您的推动器帐户并导航到您的应用程序。在
debug...<br>...<br>BROADCAST_DRIVER=pusher<br><br>PUSHER_APP_ID={YOUR_APP_ID}<br>PUSHER_APP_KEY={YOUR_APP_KEY}<br>PUSHER_APP_SECRET={YOUR_APP_SECRET}<br>PUSHER_APP_CLUSTER={YOUR_APP_CLUSTER}<br>...<br>...<br>
> consoleAppEventsNewMessageNotification
>下,您应该能够查看已记录的消息。private-user.2
> ,这将我们带到了本文的结尾!希望在我试图最大程度地简化事物时,这并不是太多了。> 今天,我们经历了Laravel -Broadcasting的最少讨论的功能之一。它允许您使用Web插座发送实时通知。在本文的整个过程中,我们建立了一个现实世界的示例,该示例证明了上述概念。>结论
以上是Laravel广播的工作方式的详细内容。更多信息请关注PHP中文网其他相关文章!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

phpoffersvarioussessionsionsavehandlers:1)文件:默认,简单的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的session是用于在服务器端保存用户数据以在多个请求之间保持状态的机制。具体来说,1)session通过session_start()函数启动,并通过$_SESSION超级全局数组存储和读取数据;2)session数据默认存储在服务器的临时文件中,但可通过数据库或内存存储优化;3)使用session可以实现用户登录状态跟踪和购物车管理等功能;4)需要注意session的安全传输和性能优化,以确保应用的安全性和效率。

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

绝对会话超时从会话创建时开始计时,闲置会话超时则从用户无操作时开始计时。绝对会话超时适用于需要严格控制会话生命周期的场景,如金融应用;闲置会话超时适合希望用户长时间保持会话活跃的应用,如社交媒体。

服务器会话失效可以通过以下步骤解决:1.检查服务器配置,确保会话设置正确。2.验证客户端cookies,确认浏览器支持并正确发送。3.检查会话存储服务,如Redis,确保其正常运行。4.审查应用代码,确保会话逻辑正确。通过这些步骤,可以有效诊断和修复会话问题,提升用户体验。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具