skoniks/php_cent

Centrifugo (Centrifuge) [1.0+] PHP 服务器 REDIS 与 HTTP API 实现适用于 Laravel 5+

v2.8 2018-11-02 16:15 UTC

This package is auto-updated.

Last update: 2024-09-29 04:14:26 UTC


README

Centrifugo (Centrifuge) [1.0+] PHP 服务器 REDIS 与 HTTP API 实现适用于 Laravel 5+ 不兼容 Centrifugo [2.0+],将稍后更新!

基本安装

  1. 运行 composer require skoniks/php_centcomposer update
  2. 创建如下所示的 config/centrifugo.php
  3. config/app.php 中添加如下所示的别名

对于 Laravel 5.5+ 使用版本 >= "2.5"

配置示例 config/centrifugo.php

centrifugo.php

别名添加 config/app.php

    'aliases' => [
        ...
        'Centrifugo'=> SKONIKS\Centrifugo\Centrifugo::class,
    ]
    

设置 Redis 作为传输

  1. 将您的 Redis 连接添加到 config/database.php
  2. config/centrifugo.php 更改为 Redis 设置

添加 Redis 连接 config/database.php

...
    'redis' => [
        ...
        'centrifugo' => [
            'host' => '127.0.0.1',
            'password' => '',
            'port' => 6379,
            'database' => 1,
        ],
    ],
...

Redis 支持的传输

请确保 HTTP 连接必须独立于 Redis 连接工作。这是因为 Redis 传输只提供以下方法

  • 'publish'
  • 'broadcast'
  • 'unsubscribe'
  • 'disconnect'

Redis 不提供这些方法

  • presence
  • history
  • channels
  • stats
  • node

模块使用示例 [发送您的请求]

<?php
use Centrifugo;
class Controller {
    public function _function(){
        // declare Centrifugo
        $centrifugo = new Centrifugo();

        // generating token example
        $current_time = time();
        $user_id = '1234567890';
        $token = Centrifugo::token($user_id, $current_time, 'custom info');
                                
        // publishing example
        $centrifugo->publish("channel" , ["custom data"]);
        
        // list of awailible methods: 
        $response = $centrifugo->publish($channel, $data);
        $response = $centrifugo->unsubscribe($channel, $user_id);
        $response = $centrifugo->disconnect($user_id);
        $response = $centrifugo->presence($channel);
        $response = $centrifugo->history($channel);
        $response = $centrifugo->channels();
        $response = $centrifugo->stats();
        $response = $centrifugo->node();
        $token = Centrifugo::token($user_id, $timestamp, $info);
                                
        // $response == false | when error
    }

更多信息请点击 这里