tanuck/pusher

Pusher库的CakePHP集成。

安装: 55

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 1

开放问题: 0

类型:cakephp-plugin

2.0.0 2015-08-16 20:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:14:57 UTC


README

Build Status License Total Downloads

CakePHP插件,用于与Pusher API交互。

安装

在你的composer.json文件中包含以下内容

{
	"require": {
		"tanuck/pusher": "dev-master"
	}
}

注意:此插件应在Plugin目录中安装,而不是composer vendors目录。

配置

首先,你必须加载插件

CakePlugin::load('CakePusher');

在控制器中包含组件

class MyController extends AppController {

	public $components = array(
		'CakePusher.Pusher' => array(
			'auth_key' => 'PUSHER_APP_AUTH_KEY',
			'secret' => 'PUSHER_APP_SECRET',
			'app_id' => 'PUSHER_APP_ID',
		),
	);
}

这是Component构造函数的最基本形式,更多配置选项请参阅下面的使用部分。

然后像这样访问组件

$this->Pusher->trigger('my-channel', 'my-event', 'My Message');

使用方法

如果你想在CakePHP应用程序中使用单个Pusher应用程序,则上面的$components定义就足够了。可用的选项与传递给Pusher API构造函数的选项相同(见这里获取更多信息)。

该插件允许你通过在组件设置中嵌套配置数组来配置和使用多个Pusher应用程序。可以通过组件设置中的数组索引作为每个Pusher应用程序的内部别名来完成。例如

class MyController extends AppController {

	public $components = array(
		'CakePusher.Pusher' => array(
			'main' => array(
				'auth_key' => 'PUSHER_APP_AUTH_KEY',
				'secret' => 'PUSHER_APP_SECRET',
				'app_id' => 'PUSHER_APP_ID',
			),
			'otherApp' => array(
				'auth_key' => 'PUSHER_APP_AUTH_KEY',
				'secret' => 'PUSHER_APP_SECRET',
				'app_id' => 'PUSHER_APP_ID',
			),
		),
	);
}

注意:当使用多个Pusher实例时,你必须指定一个main应用程序。

您可以直接在组件上使用方法与默认的main应用程序交互,或者您可以指定要使用的应用程序,使用with方法

$this->Pusher->trigger('my-channel', 'my-event', 'My Message');
$this->Pusher->socketAuth('my-channel', '1234.1234');
$this->Pusher->presenceAuth('my-channel', '1234.1234', 'user-12', true);
$this->Pusher->get('/channels');

$this->Pusher->with('otherApp')->trigger('my-channel', 'my-event', 'My Message');

您可以通过以下方式获取和设置默认Pusher应用程序的名称

$this->Pusher->getDefault();

$this->Pusher->setDefault('otherApp');

并且您可以在运行时添加额外的应用程序配置

$this->Pusher->addAppConfig('myAppName', array(
	'auth_key' => 'PUSHER_APP_AUTH_KEY',
	'secret' => 'PUSHER_APP_SECRET',
	'app_id' => 'PUSHER_APP_ID',
));

许可证

cakephp-pusher在MIT许可证下提供。