国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
創(chuàng)建事件類
Let's go ahead and walk through how you are supposed to test the use-case that到目前為止,我們已經(jīng)構(gòu)建了。
>結(jié)論
首頁(yè) 后端開(kāi)發(fā) php教程 Laravel廣播的工作方式

Laravel廣播的工作方式

Mar 05, 2025 am 09:27 AM

今天,我們將在Laravel Web框架中探索廣播的概念。當(dāng)服務(wù)器端發(fā)生某些事情時(shí),它允許您將通知發(fā)送到客戶端。在本文中,我們將使用第三方Pusher庫(kù)將通知發(fā)送到客戶端。

>

如果您曾經(jīng)想在Laravel中的服務(wù)器上發(fā)生某些事情時(shí),您正在尋找廣播功能的某些事情時(shí)將通知從服務(wù)器發(fā)送到客戶端。現(xiàn)在,當(dāng)用戶A向用戶B發(fā)送消息時(shí),您需要實(shí)時(shí)通知用戶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.讓我們了解插座的基本流程,然后再深入研究實(shí)際實(shí)施。

首先,您需要一臺(tái)支持Web-Sockets協(xié)議的服務(wù)器,允許客戶端建立Web套接字連接。
    您可以實(shí)現(xiàn)自己的服務(wù)器或使用自己的服務(wù)器或使用第三方服務(wù)。我們將更喜歡本文中的后者。
  • >客戶在成功連接時(shí)啟動(dòng)了與Web套接字服務(wù)器的Web套接字連接,并在連接成功時(shí)接收唯一的標(biāo)識(shí)符。
  • >
  • 連接成功,客戶將訂閱某些頻道,它將訂閱它想要接收事件的某些頻道。服務(wù)器端,當(dāng)發(fā)生特定事件時(shí),我們通過(guò)提供頻道名稱和事件名稱來(lái)告知Web-Socket服務(wù)器。
  • ,最后,Web-Socket Server將該事件廣播到該特定頻道上注冊(cè)的客戶端。
  • >
  • >在單個(gè)go中看起來(lái)是否太多,請(qǐng)不要擔(dān)心。當(dāng)我們?yōu)g覽本文時(shí),您將掌握它。
  • 廣播配置文件
  • >
  • 接下來(lái),讓我們查看
config/broadcasting.php

。

<?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>

的默認(rèn)廣播配置文件,默認(rèn)情況下,請(qǐng)?jiān)L問(wèn)多個(gè)core new eyan new new new nect of there norker of there corect of。使用日志適配器。當(dāng)然,如果您將推動(dòng)器

適配器用作我們的默認(rèn)廣播驅(qū)動(dòng)程序。 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.

<?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>
>

創(chuàng)建事件類

每當(dāng)您想在Laravel提出自定義事件時(shí),都應(yīng)為該事件創(chuàng)建類。基于事件的類型,Laravel做出了相應(yīng)的反應(yīng)并采取必要的操作。

如果事件是正常事件,Laravel稱之為相關(guān)的偵聽(tīng)器類。另一方面,如果事件是廣播類型的,Laravel將該事件發(fā)送到在> config/broadcasting.php

文件中配置的Web插座服務(wù)器。

當(dāng)我們?cè)谑纠惺褂胮usher服務(wù)時(shí),Laravel將在我們的示例中使用laravel將事件發(fā)送給Pusher Server。和其他必要的推動(dòng)器相關(guān)信息。

>private移動(dòng)進(jìn)一步,我們使用 private <code>user.{USER_ID} Echo的方法來(lái)訂閱私有渠道用戶。{user_id} <code>Echo。正如我們前面討論的那樣,客戶必須在訂閱私人渠道之前對(duì)自己進(jìn)行身份驗(yàn)證。因此, echo<code>user.{USER_ID}對(duì)象通過(guò)使用必要的參數(shù)在后臺(tái)發(fā)送XHR來(lái)執(zhí)行必要的身份驗(yàn)證。最后,Laravel試圖找到用戶。{user_id}<strong>路由,它應(yīng)該與我們?cè)?lt;ancoutes>文件中定義的路由相匹配。</ancoutes></strong>

>

>如果一切順利,如果一切順利,則應(yīng)將Web-Socket連接與推動(dòng)器Web-Socket Server和IT列表的 user.{USER_ID}

在我們的情況下,我們要聆聽(tīng) newMessageNotification <antemification>事件,因此我們使用了<code> listan> listan> echo> echo <y>的方法來(lái)實(shí)現(xiàn)它。為了使事情變得簡(jiǎn)單,我們只會(huì)提醒我們從Pusher Server收到的消息。<p>><code>NewMessageNotification,這就是從Web-Sockockets服務(wù)器接收事件的設(shè)置。接下來(lái),我們將在控制器文件中瀏覽 send<code>listen的方法,該方法提出了廣播事件。Echo>

>讓我們快速刪除 send <code> send<p>> <code>send方法的代碼。

>

send

在我們的情況下,我們將在收到新消息時(shí)通知登錄用戶。因此,我們?cè)噲D在 send<pre class="brush:php;toolbar:false">...&lt;br&gt;...&lt;br&gt;BROADCAST_DRIVER=pusher&lt;br&gt;&lt;br&gt;PUSHER_APP_ID={YOUR_APP_ID}&lt;br&gt;PUSHER_APP_KEY={YOUR_APP_KEY}&lt;br&gt;PUSHER_APP_SECRET={YOUR_APP_SECRET}&lt;br&gt;PUSHER_APP_CLUSTER={YOUR_APP_CLUSTER}&lt;br&gt;...&lt;br&gt;...&lt;br&gt;</pre>方法中模仿該行為。<p><code>send接下來(lái),我們使用 event <code> event

助手函數(shù)來(lái)提高 newMessageNotification <anementification>事件。由于<code> newMessAgeNotification <ante> event屬于<code> shopsbroadcastNow<code>eventtype type type type type NewMessageNotificationconfig/broadcasting.phpNewMessageNotification文件中加載默認(rèn)的廣播配置。最后,它將 newMessAgeNotification<code>ShouldBroadcastNow事件廣播到用戶上的已配置的Web-Socket服務(wù)器。在我們的情況下,該事件將廣播到<ancy>頻道上的Pusher Web-Socket服務(wù)器。 If the ID of the recipient user is <p>, the event will be broadcast over the <code>user.{USER_ID} channel.1user.1As 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到目前為止,我們已經(jīng)構(gòu)建了。

在您的瀏覽器中打開(kāi)URL https:// your-laravel-site-domain/message/index。如果您尚未登錄,您將被重定向到登錄屏幕。登錄后,您應(yīng)該看到我們之前定義的廣播視圖 - 尚無(wú)幻想。當(dāng)我們啟用了按Pusher客戶端庫(kù)提供的

>設(shè)置時(shí),它將在瀏覽器控制臺(tái)中記錄所有內(nèi)容以進(jìn)行調(diào)試。讓我們看看當(dāng)您訪問(wèn)http:// your-laravel-site-domain/message/index Page時(shí),它將記錄到控制臺(tái)的內(nèi)容。

>Pusher.logToConsole>

它已經(jīng)使用Pusher Web-Socket Server打開(kāi)了Web-Socket連接,并訂閱了自身以聆聽(tīng)私人通道上的事件。當(dāng)然,您可以根據(jù)登錄的用戶的ID具有不同的頻道名稱?,F(xiàn)在,讓我們?cè)谝苿?dòng)以測(cè)試
<?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>
>方法的過(guò)程中保持此頁(yè)面打開(kāi)。

send接下來(lái),讓我們打開(kāi)http:// your-laravel-site-domain/message/message/message/message/send url在另一個(gè)選項(xiàng)卡或其他瀏覽器中發(fā)送url。如果您要使用其他瀏覽器,則需要登錄才能訪問(wèn)該頁(yè)面。發(fā)生了。

,它告訴您,您剛剛從

>頻道上的Pusher Web-Socket Server收到了

事件。轉(zhuǎn)到您的推動(dòng)器帳戶并導(dǎo)航到您的應(yīng)用程序。在

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>下,您應(yīng)該能夠查看已記錄的消息。private-user.2

> ,這將我們帶到了本文的結(jié)尾!希望在我試圖最大程度地簡(jiǎn)化事物時(shí),這并不是太多了。>

>結(jié)論

今天,我們經(jīng)歷了Laravel -Broadcasting的最少討論的功能之一。它允許您使用Web插座發(fā)送實(shí)時(shí)通知。在本文的整個(gè)過(guò)程中,我們建立了一個(gè)現(xiàn)實(shí)世界的示例,該示例證明了上述概念。

以上是Laravel廣播的工作方式的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

如何在PHP中實(shí)施身份驗(yàn)證和授權(quán)? 如何在PHP中實(shí)施身份驗(yàn)證和授權(quán)? Jun 20, 2025 am 01:03 AM

tosecurelyhandleauthenticationandationallizationInphp,lofterTheSesteps:1.AlwaysHashPasswordSwithPassword_hash()andverifyusingspasspassword_verify(),usepreparedStatatementStopreventsqlineptions,andStoreSeruserDatain usseruserDatain $ _sessiveferterlogin.2.implementrole-2.imaccessccsccccccccccccccccccccccccc.

如何在PHP中安全地處理文件上傳? 如何在PHP中安全地處理文件上傳? Jun 19, 2025 am 01:05 AM

要安全處理PHP中的文件上傳,核心在于驗(yàn)證文件類型、重命名文件并限制權(quán)限。1.使用finfo_file()檢查真實(shí)MIME類型,僅允許特定類型如image/jpeg;2.用uniqid()生成隨機(jī)文件名,存儲(chǔ)至非Web根目錄;3.通過(guò)php.ini和HTML表單限制文件大小,設(shè)置目錄權(quán)限為0755;4.使用ClamAV掃描惡意軟件,增強(qiáng)安全性。這些步驟有效防止安全漏洞,確保文件上傳過(guò)程安全可靠。

PHP中==(松散比較)和===(嚴(yán)格的比較)之間有什么區(qū)別? PHP中==(松散比較)和===(嚴(yán)格的比較)之間有什么區(qū)別? Jun 19, 2025 am 01:07 AM

在PHP中,==與===的主要區(qū)別在于類型檢查的嚴(yán)格程度。==在比較前會(huì)進(jìn)行類型轉(zhuǎn)換,例如5=="5"返回true,而===要求值和類型都相同才會(huì)返回true,例如5==="5"返回false。使用場(chǎng)景上,===更安全應(yīng)優(yōu)先使用,==僅在需要類型轉(zhuǎn)換時(shí)使用。

如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? 如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? Jun 19, 2025 pm 05:13 PM

PHP中使用基本數(shù)學(xué)運(yùn)算的方法如下:1.加法用 號(hào),支持整數(shù)和浮點(diǎn)數(shù),也可用于變量,字符串?dāng)?shù)字會(huì)自動(dòng)轉(zhuǎn)換但不推薦依賴;2.減法用-號(hào),變量同理,類型轉(zhuǎn)換同樣適用;3.乘法用*號(hào),適用于數(shù)字及類似字符串;4.除法用/號(hào),需避免除以零,并注意結(jié)果可能是浮點(diǎn)數(shù);5.取模用%號(hào),可用于判斷奇偶數(shù),處理負(fù)數(shù)時(shí)余數(shù)符號(hào)與被除數(shù)一致。正確使用這些運(yùn)算符的關(guān)鍵在于確保數(shù)據(jù)類型清晰并處理好邊界情況。

如何與PHP的NOSQL數(shù)據(jù)庫(kù)(例如MongoDB,Redis)進(jìn)行交互? 如何與PHP的NOSQL數(shù)據(jù)庫(kù)(例如MongoDB,Redis)進(jìn)行交互? Jun 19, 2025 am 01:07 AM

是的,PHP可以通過(guò)特定擴(kuò)展或庫(kù)與MongoDB和Redis等NoSQL數(shù)據(jù)庫(kù)交互。首先,使用MongoDBPHP驅(qū)動(dòng)(通過(guò)PECL或Composer安裝)創(chuàng)建客戶端實(shí)例并操作數(shù)據(jù)庫(kù)及集合,支持插入、查詢、聚合等操作;其次,使用Predis庫(kù)或phpredis擴(kuò)展連接Redis,執(zhí)行鍵值設(shè)置與獲取,推薦phpredis用于高性能場(chǎng)景,Predis則便于快速部署;兩者均適用于生產(chǎn)環(huán)境且文檔完善。

我如何了解最新的PHP開(kāi)發(fā)和最佳實(shí)踐? 我如何了解最新的PHP開(kāi)發(fā)和最佳實(shí)踐? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

什么是PHP,為什么它用于Web開(kāi)發(fā)? 什么是PHP,為什么它用于Web開(kāi)發(fā)? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

如何設(shè)置PHP時(shí)區(qū)? 如何設(shè)置PHP時(shí)區(qū)? Jun 25, 2025 am 01:00 AM

tosetTherightTimeZoneInphp,restate_default_timezone_set()functionAtthestArtofyourscriptWithavalIdidentIdentifiersuchas'america/new_york'.1.usedate_default_default_timezone_set_set()

See all articles