vinkla/Pusher是一个用于Laravel框架的Pusher服务提供商的包。然而,它不适用于Laravel 5.4版本。Laravel 5.4版本引入了Broadcasting功能,它提供了更好的实时通信解决方案。
对于Laravel 5.4版本及以上,推荐使用Laravel自带的Broadcasting功能来实现实时通信。Broadcasting基于事件系统,可以通过广播事件来实现实时消息传递。它支持多种驱动程序,包括Pusher、Redis和Socket.io等。
如果你想在Laravel 5.4中使用实时通信功能,可以按照以下步骤进行设置:
'providers' => [
// ...
Illuminate\Broadcasting\BroadcastServiceProvider::class,
],
'aliases' => [
// ...
'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
],
'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,
],
],
],
BROADCAST_DRIVER=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
ExampleEvent
的事件类:namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
class ExampleEvent implements ShouldBroadcast
{
use SerializesModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new Channel('example-channel');
}
}
event
函数来触发事件。例如,在控制器中触发ExampleEvent
事件:namespace App\Http\Controllers;
use App\Events\ExampleEvent;
class ExampleController extends Controller
{
public function exampleMethod()
{
event(new ExampleEvent('Hello, world!'));
}
}
通过以上步骤,你可以在Laravel 5.4及以上版本中实现实时通信功能,而不需要使用vinkla/Pusher包。这样可以更好地利用Laravel自带的Broadcasting功能,并且不依赖于第三方包。
领取专属 10元无门槛券
手把手带您无忧上云