larahook/simple-centrifugo 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

larahook/simple-centrifugo

最新稳定版本:v0.0.13

Composer 安装命令:

composer require larahook/simple-centrifugo

包简介

Connection and subscription tokens for centrifugo

README 文档

README

Install

composer require larahook/simple-centrifugo

Driver

Add centrifugo connection with simple-centrifugo driver to config/broadcasting.php

'connections' => [
    'centrifugo' => [
        'driver' => 'simple-centrifugo',
        'token_hmac_secret_key' => env('CENTRIFUGO_TOKEN_HMAC_SECRET_KEY', ''),
        'api_key' => env('CENTRIFUGO_API_KEY', ''),
        'url' => env('CENTRIFUGO_URL', 'http://localhost:8000'), // centrifugo api url
        'verify' => env('CENTRIFUGO_VERIFY', false), // Verify host ssl if centrifugo uses this
        'ssl_key' => env('CENTRIFUGO_SSL_KEY', null), // Self-Signed SSl Key for Host (require verify=true)
    ],
]

Usage

Get tokens for client

  • Add SimpleCentrifugo trait to your class
  • Use method getConnectionToken for get connection token
  • And use method getSubscriptionToken for get subscription tokens
use Carbon\Carbon;
use Larahook\SimpleCentrifugo\Trait\SimpleCentrifugo;

class ChannelService
{
    use SimpleCentrifugo;

    /**
     * @param int $userId
     * @param Carbon $exp
     *
     * @return array
     */
    public function getConnToken(int $userId, Carbon $exp): array
    {
        return $this->getConnectionToken($userId, $exp);
    }

    /**
     * @param int $userId
     * @param array $channels
     * @param Carbon $exp
     *
     * @return array
     */
    public function getSubsToken(int $userId, array $channels, Carbon $exp): array
    {
        return $this->getSubscriptionToken($userId, $channels, $exp);
    }
}

Event example

Push message to centrifugo channel via laravel events

namespace App\Events;

use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Auth;

class PersonalEvent implements ShouldBroadcastNow
{
    use Dispatchable;
    use SerializesModels;

    /**
     * @param array $message
     */
    public function __construct(public array $message) {}

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['personal:#'.Auth::id()];
    }

    public function broadcastAs()
    {
        return 'PersonalEvent';
    }
}

Event execute

PersonalEvent::dispatch(['info']);

统计信息

  • 总下载量: 2.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固