×ðÁú¿­Ê±¹ÙÍøµÇ¼

Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel EchoºÍPusherʵÏÖWebSocketsͨѶ£¿

laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬¾ßÓи߶ȿÉÀ©Õ¹ÐԺ͸ßЧÐÔ£¬ËüÌṩÁËÐí¶àÇ¿Ê¢µÄ¹¤¾ßºÍ¿â£¬ÉÁ¿ª·¢Õß¿ÉÒÔ¿ìËÙ¹¹½¨¸ßÖÊÁ¿µÄwebÓ¦ÓóÌÐò¡£ÆäÖУ¬laravel echoºÍpusherÊÇÁ½¸öºÜÊÇÖ÷ÒªµÄ¹¤¾ß£¬Í¨¹ýËüÃÇ¿ÉÒÔºÜÈÝÒ×µØʵÏÖwebsocketsͨѶ£¬±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùÔÚlaravelÓ¦ÓóÌÐòÖÐʹÓÃÕâÁ½¸ö¹¤¾ß¡£

ʲôÊÇWebSockets£¿

WebSocketsÊÇÒ»ÖÖ¾ßÓÐË«ÏòͨѶµÄЭÒ飬Ëü¿ÉÒÔʵÏÖʵʱÐÔ¸ü¸ß¡¢¸ü¿ìËÙµÄÊý¾Ý´«Êä¡£Óë¹Å°åµÄHTTPÇëÇóÏà±È£¬WebSockets¿ÉÒÔ¼á³ÖÅþÁ¬µÄ״̬£¬×ÝȻûÓÐÇëÇó»òÏìÓ¦£¬Ò²¿ÉÒÔÔÚ¿Í»§¶ËºÍЧÀÍÆ÷Ö®¼ä´«ËÍÊý¾Ý¡£WebSockets¿ÉÒÔÌṩ¸üºÃµÄÓû§ÌåÑéºÍ¸ü¸ßµÄÓ¦ÓóÌÐòÐÔÄÜ£¬¿ÉÒÔ±»ÓÃÓÚ¶àÖÖʵʱӦÓó¡¾°£¬ÀýÈç̸ÌìÓ¦Óá¢ÓÎÏ·µÈµÈ¡£

Laravel EchoºÍPusher¼ò½é

Laravel EchoÊÇÒ»¸öÊÂÎñ¹ã²¥¿ò¼Ü£¬ËüÌṩÁËÒ»¸ö¼òÆÓ¡¢Ò»ÖµÄAPIÀ´ÈÃÄãÔÚ¿Í»§¶ËÅþÁ¬ÊµÊ±Í¨Ñ¶Í¨µÀ¡£EchoÒÀÀµÓÚPusher£¬µ±PusherÎÞ·¨Ê¹ÓÃʱ¿ÉÒÔʹÓÃRedis×÷ΪÌæ»»¼Æ»®¡£PusherÔòÊÇÒ»ÖÖʵʱWebSocketsЧÀÍ£¬Ëü¿ÉÒÔ×ÊÖúÄãÇáËɵؽ«ÊµÊ±¹¦Ð§Ìí¼Óµ½Ó¦ÓóÌÐòÖУ¬PusherÊÇÒ»ÖÖÍйܽâ¾ö¼Æ»®£¬ËüÔÊÐíÄ㽫ËùÓеÄWebSocketsÖÎÀíʹÃü½»¸øµÚÈý·½Ð§ÀÍ£¬ÒÔ±ã¸üºÃµØרעÓÚÓ¦ÓóÌÐòµÄÓªÒµÂß¼­¡£

ÉèÖÃLaravel EchoºÍPusher

ÔÚLaravelÏîÄ¿ÖУ¬Ê×ÏÈÐèҪװÖÃEchoºÍPusher£¬¿ÉÒÔʹÓÃcomposerÏÂÁî¾ÙÐÐ×°Öãº

composer require pusher/pusher-php-server pusher/pusher-http-laravel beyondcode/laravel-websockets
npm install --save laravel-echo pusher-js

µÇ¼ºó¸´ÖÆ

×°ÖÃÍê±ÏÖ®ºó£¬ÐèÒªÔÚ.envÎļþÖÐÉèÖÃPusherµÄÏà¹ØÐÅÏ¢£º

PUSHER_APP_ID=your-app-id
PUSHER_APP_KEY=your-app-key
PUSHER_APP_SECRET=your-app-secret
PUSHER_APP_CLUSTER=your-app-cluster

µÇ¼ºó¸´ÖÆ

È»ºóÔÚconfig/broadcasting.phpÎļþÖУ¬ÉèÖÃBroadcasterΪpusher£º

'default' => env('BROADCAST_DRIVER', 'pusher'),

'connections' => [
    'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_APP_KEY'),
        'secret' => env('PUSHER_APP_SECRET'),
        'app_id' => env('PUSHER_APP_ID'),
        'options' => [
            'cluster' => env('PUSHER_APP_CLUSTER'),
            'encrypted' => true,
        ],
    ],
],

µÇ¼ºó¸´ÖÆ

ʹÓÃPusherµÄLaravel WebSocketsÔËÐÐwebsocketЧÀÍÆ÷

PusherÓÐÒ»¸ö¿ªÔ´¿âLaravel WebSockets£¬ÈÃÄã¿ÉÒÔÔÚ×Ô¼ºµÄЧÀÍÆ÷ÉÏÔËÐÐWebSocketsЧÀÍÆ÷£¬´Ó¶ø½ÚÔ¼±¾Ç®²¢Ìá¸ßÐÔÄܺÍÎÞаÐÔ¡£Ïêϸ²Ù×÷ÈçÏ£º

×°ÖÃÔËÐÐLaravel WebSocketsËùÐèµÄÊý¾Ý¿â±íºÍÊý¾Ý

php artisan websocket:install

µÇ¼ºó¸´ÖÆ

Ìî³äLaravel WebSocketsËùÐèµÄÉèÖÃ

php artisan websocket:publish

µÇ¼ºó¸´ÖÆ

½¨ÉèÒ»¸ö¼àÌýÆ÷(EventServiceProviderÖÐ)

protected $listen = [
    'AppEventsExampleEvent' => [
        'AppListenersExampleListener',
    ],
];

µÇ¼ºó¸´ÖÆ

´¦ÀíÊÂÎñ

<?php

namespace AppEvents;

use IlluminateBroadcastingInteractsWithSockets;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;

class ExampleEvent
{
 use Dispatchable, InteractsWithSockets, SerializesModels;

 public $data;

 public function __construct($data)
 {
     $this->data = $data;
 }

 public function broadcastOn()
 {
     return ['test_channel'];
 }

 public function broadcastAs()
 {
     return 'example-event';
 }
}

µÇ¼ºó¸´ÖÆ

È»ºó£¬¿ÉÒÔʹÓÃÏÂÃæµÄ´úÂëÍÆËÍÊÂÎñ£º

use AppEventsExampleEvent;
use IlluminateSupportFacadesEvent;

Event::dispatch(new ExampleEvent(¡®Example payload¡¯, ¡®Another example payload¡¯));

µÇ¼ºó¸´ÖÆ

ÔõÑùÔÚÇ°¶ËʹÓÃLaravel EchoºÍPusher

Ç°¶ËʹÓÃLaravel EchoºÍPusherºÜÊǼòÆÓ£¬ÒÔÏÂÊÇÒ»¸ö»ù±¾µÄʾÀý£º

import Echo from 'laravel-echo'
import Pusher from 'pusher-js'

let pusher = new Pusher(process.env.MIX_PUSHER_APP_KEY, {
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true,
})
let echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true,
    auth: {
        headers: {
            ¡®Authorization¡¯: ¡®Bearer {access_token}¡¯,
        },
    },
})

µÇ¼ºó¸´ÖÆ

ͨ¹ýÒÔÉÏ·½·¨£¬ÎÒÃǾͿÉÒÔʵÏÖÔÚLaravelÓ¦ÓóÌÐòÖÐʹÓÃLaravel EchoºÍPusherʵÏÖWebSocketsͨѶ¡£

×ܽá

Laravel EchoºÍPusherÌṩÁ˺ÜÊÇÀû±ã¡¢¿ì½ÝµÄWebSockets½â¾ö¼Æ»®£¬ÈÃÎÒÃÇ¿ÉÒÔ¿ìËÙ¹¹½¨¸ßÐÔÄÜ¡¢ÊµÊ±µÄWebÓ¦ÓóÌÐò¡£±¾ÎÄÖÐÏêϸÏÈÈÝÁËÔõÑùÉèÖÃLaravel EchoºÍPusher£¬²¢Ê¹ÓÃPusherµÄLaravel WebSocketsÔÚÍâµØ´î½¨WebSocketsЧÀÍÆ÷£¬Í¬Ê±ÔÚÇ°¶ËʹÓÃLaravel EchoºÍPusherÅþÁ¬WebSocketsЧÀÍÆ÷£¬ÊµÏÖʵʱͨѶÄÜÁ¦¡£

ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel EchoºÍPusherʵÏÖWebSocketsͨѶ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ