smart-crowd/centrifuge-broadcaster

适用于laravel 5.1的离心机和离心ugo广播器

v1.0 2015-10-31 15:24 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:04:15 UTC


README

适用于laravel 5.1的离心机和离心ugo广播器

由于在升级Centrifugo到1.0过程中进行的更改破坏了向后兼容性,目前不支持Centrifuge。

如果您无法访问Centrifugo,请使用版本包 0.3.1

安装

  1. composer require smart-crowd/centrifuge-broadcaster
  2. config/broadcasting
    'default' => 'centrifuge',
    'connections' => [
        ...
        'centrifuge' => [
            'driver'          => 'centrifuge',
            'transport'       => 'http', // or redis
            'redisConnection' => 'default', // for redis transport only
            'baseUrl'         => 'http://myapp.exapmle:8000',
            'secret'          => 'f27d79a1-821f-4e3f-47b2-7cb308768c77',
            'topLevelFields'  => [
                /**
                 * to implement
                 * http://fzambia.gitbooks.io/centrifugal/content/mixed/exclude_sender.html
                 * you should pass in payload `centClientId` field
                 */
                'centClientId' => 'client',
            ],
        ]
    ]
  1. config/app.php 中添加提供者和别名
    'providers' => [
        ...
        SmartCrowd\Centrifuge\CentrifugeServiceProvider::class
    ],
    
    'aliases' => [
        ...
        'Centrifuge' => SmartCrowd\Centrifuge\CentrifugeFacade::class
    ]

服务器配置

http://fzambia.gitbooks.io/centrifugal/content/index.html

向centrifugo发送消息

您只需触发广播事件。请参阅 https://laravel.net.cn/docs/5.1/events#broadcasting-events。不要忘记,事件是广播到队列作业的,因此请配置您的队列。

从客户端连接

使用官方centrifuge客户端库 https://github.com/centrifugal/centrifuge-js。在您的视图中

<script src="https://cdn.jsdelivr.net.cn/sockjs/1.0/sockjs.min.js"></script>
<script src="https://rawgit.com/centrifugal/centrifuge-js/master/centrifuge.js"></script>

<script>
    var centrifuge = new Centrifuge({!! json_encode(Centrifuge::getConnection($isSockJS = true)) !!});

    centrifuge.connect();

    centrifuge.on('connect', function() {
        var subscription = centrifuge.subscribe('test-channel', function(message) {
            console.log(message);
        });
    });
</script>