美文网首页
centrifugo php使用

centrifugo php使用

作者: 567f84810acc | 来源:发表于2020-01-13 10:45 被阅读0次

centrifugo

文档地址 :https://centrifugal.github.io/centrifugo/

Centrifugo是一种与语言无关的实时消息服务器。语言无关意味着您的应用程序在前端或后端使用哪种编程语言无关紧要 - Centrifugo可以与任何一种语言协同工作。

实时消息是在事件发生后几乎立即传递给应用程序用户的消息 - 想想实时评论,聊天,实时图表,动态计数器和游戏。

Centrifugo现在支持几种实时消息传输:

带有JSON或二进制Protobuf协议的Websocket
SockJS - 尝试首先建立Websocket连接的库,然后在Websocket连接出现问题时自动回退到HTTP传输(服务器发送事件,XHR流,XHR轮询等)

项目的动机¶
Centrifugo最初诞生的目的是帮助用语言或框架编写的应用程序,而不需要内置的concurency支持来引入实时更新。例如,Django,Flask,Yii,Laravel,Ruby on Rails等框架对使用许多持久连接的支持很差。Centrifugo旨在帮助解决这个问题,并继续用您最喜欢的语言和最喜欢的框架编写后端。它还具有一些功能(性能,可伸缩性,连接管理,重新连接时的消息恢复等),即使您使用异步并发语言编写后端,也可以简化您作为开发人员的生活。

概念¶
Centrifugo作为独立服务器运行,负责处理来自应用程序用户的持久连接。您的应用程序后端和前端可以用任何编程语言编写。您的客户端使用应用程序后端提供的连接令牌(JWT)从前端连接Centrifugo并订阅频道。一旦发生某些事件,您的应用程序后端就可以使用Centrifugo API将包含事件的消息发布到通道中。该消息将发送给当前在频道上订阅的所有客户。实际上Centrifugo是一个面向用户的PUB / SUB服务器。

安装

支持

安装方式 (源码构建)

git clone https://github.com/centrifugal/centrifugo.git

cd centrifugo

环境

go version go1.11.4 darwin/amd64

go mod

go mod init

// go mod init (name) ## name 自己定义 这里使用空

下载依赖

go mod vendor

go mod download

过程中需要* F *墙*

go build

生成 二进制文件
# https://centrifugal.github.io/centrifugo/server/install/

配置文件

# https://centrifugal.github.io/centrifugo/server/configuration/
{
  "admin":true, # 开启web
  "secret": "secret",  # api 生成token
  "api_key":"api_key", # api 使用授权的apikey
  "admin_password": "web密码",
  "admin_secret": "admin->secret",
  "client_channel_limit":2, #设置单个客户端可以拥有的最大不同通道订阅数。
  "channel_max_length":255, #设置通道名称的最大长度。 默认255
  "client_anonymous":true, #匿名(具有空用户ID),只能订阅anonymous启用了选项的通道。
  "namespaces": [  ## 命名空间
    {
      "name": "abbb",
      "anonymous": false,
      "publish": true,
      "join_leave": true,
      "presence": true,
      "presence_stats": true,
      "history_size": 0,
      "history_lifetime": 0,
      "history_recover": false
    },
    {
      "name": "public", # channel 使用 【public:test】 那么使用 此配置项
      "anonymous": true, # 支持 匿名 【空用户ID ->生产的token】
      "publish": false, # 客户端 是否支持 发送消息
      "join_leave": false, # 启用/禁用发送加入(离开)消息。默认情况下false
      "presence": true, # 支持 频道的状态信息
      "presence_stats": true, # 统计客户端信息
      #  历史配置一起使用
      "history_size": 10, #支持历史消息 条数
      "history_lifetime": 60, #支持历史消息限制时间 *秒
      "history_recover": true # 客户端 意外断开 历史数据恢复
    }
  ]
}

PHP Server Api

composer require sl4mmer/phpcent

https://github.com/centrifugal/phpcent
https://github.com/oleh-ozimok/php-centrifugo/tree/v1.0
https://github.com/oleh-ozimok/php-centrifugo

服务封装

<?php

namespace App\Services;

use App\Common\Functions;
use Illuminate\Foundation\Bus\DispatchesJobs;
/**
 * 离心机推送websocket
 * Class BlackHoleService
 * @package App\Services
 */
class BlackHoleService
{
    //DispatchesJobs
    use DispatchesJobs;
    private $Centrifugo;

    public function __construct(){
        $this->Centrifugo = new \phpcent\Client(env('CENT_URL',"http://host:8000/api"),env('CENT_API_KEY','apikey'),env('CENT_SECRET','secret'));
    }

    /**
     * 推送数据
     * @param $channel
     * @param $data
     * @return array
     */
    public function pubData($channel,$data)
    {
        try{
            if(empty($channel)){
                throw new \Exception("channel is nil.");
            }
            if(empty($data) ){
                throw new \Exception("data is nil.");
            }elseif(!is_array($data)){
                throw new \Exception("data Must be an array .");
            }
            $res = $this->Centrifugo->publish("{$channel}", $data);
            Functions::SysLog('BlackHoleService','== pubData SUC ==',[$channel,$data,$res]);
            return [true,$res];
        }catch (\Exception $e){
            Functions::SysLog('BlackHoleService','== pubData Exception ==',[[$channel,$data],$e->getMessage(),$e->getLine(),$e->getCode()]);
           return [false,$e->getMessage()];
        }
    }

    /**
     * 发送广播 $channels 空格 间隔
     * @param $channels
     * @param $data
     * @return array
     */
    public function pubBroadcast($channels, $data)
    {
        try{
            if(empty($channel)){
                throw new \Exception("channels is nil.");
            }
            if(empty($data) ){
                throw new \Exception("data is nil.");
            }elseif(!is_array($data)){
                throw new \Exception("data Must be an array .");
            }
            $res =  $this->Centrifugo->broadcast($channels, $data);
            Functions::SysLog('BlackHoleService','== pubBroadcast SUC ==',[$channels,$data,$res]);
            return [true,$res];
        }catch (\Exception $e){
            Functions::SysLog('BlackHoleService','== pubBroadcast Exception ==',[$e->getMessage(),$e->getLine(),$e->getCode()]);
            return [false,$e->getMessage()];
        }
    }
    /**
     * 获取授权token 默认 userId 空支持 anonymous
     * @param string $userId
     * @param int $exp
     * @param array $info
     * @return array
     */
    public function getToken($userId='',$exp=0,$info=[])
    {
        try{
            $token = $this->Centrifugo->generateConnectionToken($userId,$exp,$info);
            return [true,$token];
        }catch (\Exception $e){
            Functions::SysLog('BlackHoleService','== GetToken Exception ==',[[$userId,$exp,$info],$e->getMessage(),$e->getLine(),$e->getCode()]);
            return [false,$e->getMessage()];
        }
    }

    /**
     * 获取通道
     * @return mixed
     */
    public function getChannels(){
        try{
            $response = $this->Centrifugo->channels();
            return [true,Functions::object_array($response->result->channels)];
        }catch (\Exception $e){
            Functions::SysLog('BlackHoleService','== getChannels Exception ==',[$e->getMessage(),$e->getLine(),$e->getCode()]);
            return [false,$e->getMessage()];
        }
    }

    /**
     * 获取通道下的历史数据
     * @param $channel
     * @return array
     */
    public function getHistory($channel){
        try{
            $response = $this->Centrifugo->history($channel);
            return [true,Functions::object_array($response->result->publications)];
        }catch (\Exception $e){
            Functions::SysLog('BlackHoleService','== getHistory Exception ==',[$e->getMessage(),$e->getLine(),$e->getCode()]);
            return [false,$e->getMessage()];
        }
    }

    /**
     * 获取通道下的客户端
     * @param $channel
     * @return array
     */
    public function getClientsByChannel($channel){
        try{
            $response = $this->Centrifugo->presence($channel);
           return [true,Functions::object_array($response->result->presence)];
        }catch (\Exception $e){
            Functions::SysLog('BlackHoleService','== getClientsByChannel Exception ==',[$e->getMessage(),$e->getLine(),$e->getCode()]);
            return [false,$e->getMessage()];
        }
    }

    /**
     * 获取通道的统计信息
     * @param $channel
     * @return array
     */
    public function getStatsByChannel($channel){
        try{
            $response = $this->Centrifugo->presence_stats($channel);
            return [true,Functions::object_array($response->result)];
        }catch (\Exception $e){
            Functions::SysLog('BlackHoleService','== getStatsByChannel Exception ==',[$e->getMessage(),$e->getLine(),$e->getCode()]);
            return [false,$e->getMessage()];
        }
    }

}

来源 :www.alonexy.com

相关文章

  • centrifugo php使用

    centrifugo 文档地址 :https://centrifugal.github.io/centrifugo...

  • 网页消息组件之: Centrifugo

    地址 什么是Centrifugo Centrifugo 是一种与语言无关的可扩展实时消息传递服务器。 Centri...

  • MySQL批量处理

    一、使用PHP exec 启动 php 服务,出现 [php] 程序中,禁止使用此函数二、 大...

  • PHP 疑难杂症

    PHP 使用unsigned bigint PHP 使用unsigned bigint作为主键时应该使用PHPx6...

  • 2018-09-12

    php优化的方法 1、服务器使用Linux系统 2、使用Nginx或Apache来运行PHP 3、开始使用PHP7...

  • 认识php

    认识php 在网页中使用php输出helloword PHP 页面 ...

  • mac php xdebug3

    重启php brew services restart php php 查看相关信息 使用 php -i 查看ph...

  • PHP 类自动加载机制

    版本 PHP5.1.2 之前使用 __autoload() 。PHP5.1.2 之后使用 spl_autoload...

  • iOS推送服务端实现

    推送服务端代码PHP,如果使用中出现问题,建议找PHP的同学一起阅读使用终端命令$ php 路径/push.php...

  • php操作数据库

    PHP操作MySQL 每日目标 能够使用php连接MySQL数据库 能够使用php对MySQL进行查询操作 能够使...

网友评论

      本文标题:centrifugo php使用

      本文链接:https://www.haomeiwen.com/subject/mhygactx.html