Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel EchoʵÏÖWebSocketsͨѶ£¿
laravelÊÇÒ»ÖÖÊ¢ÐеÄphp¿ò¼Ü£¬ËüÓÐÐí¶àÓÐÓõŤ¾ßºÍÀ©Õ¹¿â£¬¿ÉÒÔÉÁ¿ª·¢ÕßÇáËɹ¹½¨¸ßÖÊÁ¿µÄwebÓ¦ÓóÌÐò¡£ÆäÖУ¬laravel echoÊÇÒ»¿îÇ¿Ê¢µÄwebsocketsͨѶ¹¤¾ß£¬¿ÉÒÔ×ÊÖú¿ª·¢ÕßÔÚwebÓ¦ÓóÌÐòÖÐʵÏÖʵʱͨѶ¡£
ÔÚ±¾ÆªÎÄÕÂÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃLaravel Echo²¢½¨ÉèWebSocketsЧÀÍÆ÷À´ÊµÏÖʵʱͨѶ¡£ÎÒÃǽ«Ê×ÏȼòÊöʲôÊÇWebSocketsºÍLaravel Echo£¬½Ó×ÅÎÒÃǽ«½â˵ÔõÑù×°ÖúÍÉèÖÃLaravel Echo£¬×îºóÑÝʾÔõÑùʹÓÃLaravel EchoÓëJavaScript¿Í»§¶ËͨѶ¡£
ʲôÊÇWebSockets£¿
WebSocketsÊÇÒ»ÖÖÐÒ飬ËüÔÊÐíWebÓ¦ÓóÌÐòºÍWebЧÀÍÆ÷Ö®¼ä½¨ÉèʵʱͨѶÅþÁ¬¡£ÕâÖÖÅþÁ¬¿ÉÒÔ¼á³Ö³¤Ê±¼ä¿ªÆô£¬²¢ÇÒÊÇË«ÏòµÄ£¬¿ÉÒÔͬʱ¾ÙÐжÁÈ¡ºÍдÈë¡£
ÓëHTTPÇëÇó²î±ð£¬WebSocketsÅþÁ¬µÄÒ»Á¬¿ªÆô¿ÉÒÔʹЧÀͶËʵʱµØÍÆËÍÊý¾Ýµ½¿Í»§¶Ë£¬¶ø²»ÐèҪͨ¹ýÂÖѯ»ò³¤ÂÖѯÀ´ÊµÏÖ¡£ÕâʹµÃWebÓ¦ÓóÌÐòÄܹ»ÊµÏÖ¿ìËÙ¡¢¸ßЧ¡¢ÊµÊ±µÄͨѶ¡£
ʲôÊÇLaravel Echo£¿
Laravel EchoÊÇLaravel¿ò¼ÜµÄÒ»¿î¹Ù·½À©Õ¹¿â£¬¿ÉÒÔ×ÊÖú¿ª·¢ÕßÔÚWebÓ¦ÓóÌÐòÖÐʵÏÖWebSocketsͨѶ¡£ËüÌṩÁËÒ»¸ö¼òÆÓµÄAPI£¬¿ÉÒÔʹÓÃJavaScriptÇáËɶ©ÔĹ㲥ƵµÀ£¬²¢ÔÚ¿Í»§¶ËºÍЧÀͶËÖ®¼äת´ïÐÂÎÅ¡£
ʹÓÃLaravel EchoºÍLaravelÍÆËÍÆ÷¿ÉÒÔÇáËɵؽ¨ÉèWebSocketsЧÀÍÆ÷£¬²¢Ê¹Óù㲥ƵµÀÀ´×éÖ¯WebSocketsͨѶ¡£ÔÚ¿Í»§¶Ë£¬ÎÒÃÇ¿ÉÒÔʹÓÃJavaScriptºÍLaravel EchoʵʱµØ¼àÌýÐÂÎÅ£¬ÒÔ±ãÔÚWebÓ¦ÓóÌÐòÖÐʵÏÖʵʱͨѶ¡£
×°ÖúÍÉèÖÃLaravel Echo
ÔÚ×îÏÈʹÓÃLaravel Echo֮ǰ£¬ÎÒÃÇÐèҪװÖúÍÉèÖÃËùÐèµÄÈí¼þ£ºLaravelºÍpusher PHP SDK¡£Laravel EchoºÍpusher PHP SDK¿ÉÒÔͨ¹ýComposer°ü¹ÜÀíÆ÷À´×°Öá£
Ê×ÏÈÐèҪװÖÃLaravel£º
composer create-project laravel/laravel your-project-name
µÇ¼ºó¸´ÖÆ
½Ó×Å£¬ÎÒÃÇÐèҪװÖÃpusher PHP SDK¡£¿ÉÒÔͨ¹ýÔËÐÐÒÔÏÂÏÂÁîÀ´Íê³Épusher PHP SDKµÄ×°Öãº
composer require pusher/pusher-php-server
µÇ¼ºó¸´ÖÆ
ΪÁ˼¯³ÉLaravel EchoºÍpusher PHP SDK£¬ÎÒÃÇÐèÒªÔÚconfig/app.phpÎļþÖÐÌí¼ÓЧÀÍÌṩ³ÌÐòºÍÓÖÃû£º
// config/app.php 'providers' => [ // ... LaravelBroadcastingBroadcastServiceProvider::class, ], 'aliases' => [ // ... 'Broadcast' => LaravelBroadcastingBroadcastFacade::class, ],
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÈÃÎÒÃÇ×öһЩÉèÖÃÊÂÇ飬ÒÔÈ·±£Laravel EchoºÍpusher PHP SDKÄܹ»Õý³£ÊÂÇé¡£ÎÒÃÇÐèÒªÔÚ.envÎļþÖÐÌí¼ÓLaravel EchoºÍpusher PHP SDKËùÐèµÄËùÓÐÉèÖãº
BROADCAST_DRIVER=pusher PUSHER_APP_ID=your-pusher-app-id PUSHER_APP_KEY=your-pusher-app-key PUSHER_APP_SECRET=your-pusher-app-secret PUSHER_APP_CLUSTER=your-pusher-app-cluster
µÇ¼ºó¸´ÖÆ
½¨ÉèWebSocketsЧÀÍÆ÷
½ÓÏÂÀ´£¬ÎÒÃǽ«Ê¹ÓÃLaravel EchoºÍpusher PHP SDKÀ´½¨ÉèWebSocketsЧÀÍÆ÷¡£ÎÒÃÇÐèÒªÔÚroutes/channels.phpÎļþÖнç˵¹ã²¥ÆµµÀ¡£¹ã²¥ÆµµÀ½«¾öÒéÄÄЩÓû§¿ÉÒÔÎüÊÕµ½¹ã²¥ÐÂÎÅ¡£
// routes/channels.php use IlluminateSupportFacadesBroadcast; Broadcast::channel('chat.{roomId}', function ($user, $roomId) { // ... });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪ¡°chat¡±µÄ¹ã²¥ÆµµÀ£¬²¢¸øËüת´ïÁËÒ»¸ö²ÎÊý¡°roomId¡±¡£Ö»ÓÐÓµÓС°chat.roomId¡±È¨ÏÞµÄÓû§²Å»ªÎüÊÕµ½¸Ã¹ã²¥ÆµµÀµÄÐÂÎÅ¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½ç˵¹ã²¥ÊÂÎñºÍÍÆËÍÆäÐÂÎÅ¡£ÔÚapp/EventsĿ¼Ï½¨ÉèÒ»¸öеÄÊÂÎñÀ࣬ÀýÈ磺
// app/Events/ChatMessageSent.php <?php namespace AppEvents; use IlluminateBroadcastingPrivateChannel; use IlluminateContractsBroadcastingShouldBroadcast; use IlluminateFoundationEventsDispatchable; use IlluminateQueueSerializesModels; class ChatMessageSent implements ShouldBroadcast { use Dispatchable, SerializesModels; public $message; public function __construct($message) { $this->message = $message; } public function broadcastOn() { return new PrivateChannel('chat.' . $this->message['room_id']); } }
µÇ¼ºó¸´ÖÆ
Õâ¸öÊÂÎñÀà°üÀ¨Á˹㲥ÊÂÎñµÄÂß¼£¬²¢ÇÒÐèҪʵÏÖShouldBroadcast½Ó̸·æÆø±»¹ã²¥³öÈ¥¡£
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔÔÚÓ¦ÓóÌÐòÖÐʵÀý»¯¸ÃÊÂÎñ£¬È»ºó·¢Ë͹㲥ÐÂÎÅ¡£ÀýÈ磬ÔÚapp/Http/Controllers/ChatController.phpÎļþÖÐÌí¼ÓÒÔÏÂÐÂÎÅ·¢ËÍÒªÁ죺
// app/Http/Controllers/ChatController.php <?php namespace AppHttpControllers; use AppEventsChatMessageSent; use IlluminateHttpRequest; class ChatController extends Controller { public function sendMessage(Request $request) { $message = [ 'user_id' => $request->user()->id, 'room_id' => $request->get('room_id'), 'message' => $request->get('message'), ]; event(new ChatMessageSent($message)); return response()->json(['status' => 'Message Sent!']); } }
µÇ¼ºó¸´ÖÆ
Çë×¢ÖØ£¬ÉÏÃæµÄ´úÂëÖУ¬event(new ChatMessageSent($message))´¥·¢ÁËÊÂÎñ£¬²¢·¢ËÍÁËÒ»¸ö°üÀ¨ÐÂÎÅÊý¾ÝµÄ¹ã²¥ÊÂÎñ¡£
½¨ÉèʵʱͨѶ
×îºó£¬ÎÒÃÇÐèÒªÔÚJavaScript¿Í»§¶ËÖÐʹÓÃLaravel Echo¼àÌý¹ã²¥ÊÂÎñ£¬ÒÔ»ñȡʵʱµÄWebSocketsͨѶ¡£ÔÚJavaScriptÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÁ½ÖÖ·½·¨À´¼àÌý¹ã²¥ÊÂÎñ£º
Echo.channel(channelName).listen(eventName, callback)£º¶©ÔĹ㲥ÊÂÎñ£¬²¢×¢²á»Øµ÷º¯Êý£¬ÒÔ±ãÔÚÎüÊÕµ½ÊÂÎñʱִÐС£
Echo.private(channelName).listen(eventName, callback)£º¶©ÔÄ˽Óй㲥ÊÂÎñ£¬²¢×¢²á»Øµ÷º¯Êý£¬ÒÔ±ãÔÚÎüÊÕµ½ÊÂÎñʱִÐС£
ÀýÈ磬ÔÚ×ðÁú¿Ê±Ì¸ÌìÓ¦ÓóÌÐòÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´¼àÌýÐÂÐÂÎŵÄÊÂÎñ£º
// resources/js/app.js import Echo from 'laravel-echo'; window.Pusher = require('pusher-js'); window.Echo = new Echo({ broadcaster: 'pusher', key: process.env.MIX_PUSHER_APP_KEY, cluster: process.env.MIX_PUSHER_APP_CLUSTER, encrypted: true, }); window.Echo.channel(`chat.${roomId}`) .listen('ChatMessageSent', (e) => { console.log(e); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃLaravel Echo¿Í»§¶Ë¶©ÔÄ¡°chat.roomId¡±ÆµµÀ£¬²¢ÔÚÎüÊÕµ½¡°ChatMessageSent¡±ÊÂÎñʱ´òӡʧÊÂÎñÊý¾Ý¡£
½áÂÛ
ͨ¹ýʹÓÃLaravel EchoºÍpusher PHP SDK£¬ÎÒÃÇ¿ÉÒÔÇáËɵؽ¨ÉèWebSocketsЧÀÍÆ÷£¬²¢Ê¹Óù㲥ƵµÀÀ´ÊµÏÖWebSocketsͨѶ¡£ÔÚ¿Í»§¶Ë£¬ÎÒÃÇ¿ÉÒÔʹÓÃJavaScriptºÍLaravel EchoʵʱµØ¼àÌýÐÂÎÅ£¬´Ó¶øʵÏÖʵʱͨѶ¡£
±ðµÄ£¬Laravel Echo»¹ÌṩÁËÐí¶àÆäËû¿ÉÓõĹ㲥ƵµÀºÍÊÂÎñ£¬ÎÒÃÇ¿ÉÒÔʹÓÃËüÃÇÀ´¹¹½¨ÖØ´óµÄWebÓ¦ÓóÌÐò¡£ÈôÊÇÄãÕýÔÚÑ°ÕÒÒ»ÖÖÏÖ´ú»¯µÄʵʱͨѶ½â¾ö¼Æ»®£¬Laravel EchoºÍpusher PHP SDKÊÇÒ»¸ö²»´íµÄÑ¡Ôñ¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel EchoʵÏÖWebSocketsͨѶ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡