Laravel-如何注册自定义广播电台 [英] Laravel - How to register custom broadcaster

查看:73
本文介绍了Laravel-如何注册自定义广播电台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在无需更改内部框架代码的情况下向BroadcastManager注册自定义广播者...

I want to register a custom broadcaster with the BroadcastManager without having to change the internal framework code...

现在我必须在Illuminate\Broadcasting\BroadcasterManager类中做类似的事情:

Now I have to do something like this in the Illuminate\Broadcasting\BroadcasterManager class:

protected function createMyCustomDriver(array $config) {
  // return instance....
}

但是有一个extend方法,但是我不知道这是该用例还是如何使用...

There is an extend method however, but I don't know if it's ment for this use case or how to use it...

目标是使用使用ZMQ的Broadcaster实现将这些广播的事件发送到WebSocket php服务器实例.

The goal is to use a Broadcaster implementation that uses ZMQ to send these broadcasted events to the WebSocket php server instance.

任何帮助表示赞赏!

链接到api文档 http://laravel.com/api/5.1/Illuminate/Broadcasting/BroadcastManager.html

推荐答案

您需要使用服务提供商来扩展Illuminate\Broadcasting\BroadcastManager\BroadcastManager.这与添加自定义防护非常相似,但这是一个超级基本的示例:

You need to extend Illuminate\Broadcasting\BroadcastManager\BroadcastManager using a service provider. This is pretty similar to adding a custom guard but here's a super basic example:

创建一个新的服务提供商,我称为我的BroadcastServiceProvider,并将以下内容添加到boot方法中:

Create a new service provider, I've called mine BroadcastServiceProvider, and add the following to the boot method:

/**
 * Bootstrap the application services.
 *
 * @param BroadcastManager $broadcastManager
 */
public function boot(BroadcastManager $broadcastManager)
{
    $broadcastManager->extend('slack', function (Application $app, array $config) {
        return new Slack;
    });
}

所有要做的就是将您的广播驱动程序(实现Illuminate\Contracts\Broadcasting\Broadcaster接口的类,在我的示例中为Slack)添加到名称为slack的广播管理器中(您可以随便叫广播者)

All that does is add your broadcast driver (a class which implements the Illuminate\Contracts\Broadcasting\Broadcaster interface which in my example is Slack) to the broadcast manager with the name slack (you can call your broadcaster anything you like).

确保将此服务提供商添加到app.php配置文件中.

Make sure you add this service provider to you app.php config file.

然后,在您的broadcasting.php配置文件中,将新驱动程序添加为连接.我的看起来像这样:

Then, in your broadcasting.php config file add your new driver as a connection. Mine looks something like this:

'connections' => [

    'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_KEY'),
        'secret' => env('PUSHER_SECRET'),
        'app_id' => env('PUSHER_APP_ID'),
    ],

    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
    ],

    'log' => [
        'driver' => 'log',
    ],

    'slack' => [
        'driver' => 'slack'
    ]

],

您会注意到驱动程序名称与服务提供商extend调用中的名称相同.您可以随意调用连接,也可以添加其他参数,如果需要,可以将这些参数传递给服务提供商.

You'll notice that the driver name is the same as what's in the service provider extend call. You can call the connection anything you like really and you can add extra parameters which are passed into the service provider should you need them.

之后,您的自定义广播公司已注册并可以使用!

After that, your custom broadcaster is registered and ready for use!

这篇关于Laravel-如何注册自定义广播电台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆