ali1 / cakephp-pusher
CakePHP 插件,用于使用 Pusher API
Requires
This package is auto-updated.
Last update: 2020-03-17 20:39:28 UTC
README
感谢 PCreations,本插件是从其分支出来的,但似乎没有维护。此应用程序具有更好的、更容易使用的私聊安全功能和更新的 Pusher 库。
此插件通过[通用 PHP Pusher 库](https://github.com/pusher/pusher-php-server)提供了对[Pusher API](http://pusher.com/)的简单访问。此插件还提供了一种通过 Helper 生成所有 JavaScript 代码的方法
安装
可以将此存储库的内容复制到您的插件文件夹中,或者将以下内容添加到 composer 中
"ali1/cakephp-pusher": "dev-master"
现在在您的 bootstrap 文件中加载它
CakePlugin::load('Pusher', array('
'bootstrap' => true
'));
在您的 behaviors/helpers 列表中添加 PusherBehavior 和 PusherHelper
//YourModel.php
public $actsAs = array('Pusher.Pusher');
//YourController.php
public $helpers = array('Pusher.Pusher');
您的应用程序需要在[Pusher 网站](http://pusher.com/)上注册。您需要将以下行添加到您的 Config/bootstrap.php 文件中
Configure::write(array(
'Pusher' => array(
'credentials' => array(
'appKey' => 'YOUR_KEY',
'appSecret' => 'YOUR_APP_SECRET',
'appId' => 'YOUR_APP_ID'
)
)
));
如何使用
触发事件
在频道上触发事件非常简单。这是一个服务器端流程。在您的控制器中只需执行以下操作
//Some event information
$data = array('message' => 'Something happened');
//Trigger an event named EVENT_NAME on the CHANNEL_NAME channel. You can use private and presence channel by prefixing the name by private- or presence-. See pusher docs (http://pusher.com/docs/client_api_guide/client_channels) for details
$this->YourModel->trigger(CHANNEL_NAME, EVENT_NAME, $data);
订阅频道
“接收”推送事件是一个客户端流程,使用 Pusher Helper 生成 JavaScript 代码以享受实时功能
$this->Pusher->subscribe(CHANNEL_NAME);
//The third argument receive string will be parsed as javascript.
$this->Pusher->bindEvent(CHANNEL_NAME, EVENT_NAME, "console.log('An event was triggered with message '+data.message+');");
使用私有频道进行身份验证
当使用私有频道时,身份验证由 Pusher 控制器处理。但是,您必须在 AppController 中提供一个方法来验证访问私有频道的请求。此频道必须返回一个布尔值(true 或 false)。
//Controller/AppController.php
public function _pusherCanAccessPrivateChannel($channelName, $socketId) {
// handle user channels e.g. private-user-4324
if (preg_match('/private-user-([0-9]+)/i', channelName, $matches)) {
if ($matches[1] == $this->Auth->user('id')) {
return true;
} else {
return false;
}
}
return false; // unrecognised channel name
}
示例
一个非常简单的例子可能是这个
//Model/FooModel.php
public $actsAs = array('Pusher.Pusher');
//Controller/FooController.php
public $components = array('Auth', 'Pusher.Pusher');
public $helpers = array('Pusher.Pusher');
public function push() {
$data = array(
'message' => 'Something happened !',
'triggeredBy' => $this->Auth->user('username')
);
$this->Foo->trigger('private-my-great-channel', 'foo_bar', $data);
}
public function receive() {
}
//View/Foo/receive.ctp
$this->Pusher->subscribe('private-my-great-channel');
$this->Pusher->bindEvent('private-my-great-channel', 'foo_bar', 'console.log("An event was triggered by "+data.triggeredBy+" with message "+data.message+);');
//Controller/AppController.php
protected function _pusherCanAccessPrivateChannel($channelName, $socketId) {
return (bool) ($this->Auth->user()); // allow access to any channel if user logged in
}
打开您的浏览器,在一个小部件中打开 yourapp/foo/receive,在另一个小部件中打开 yourapp/foo/push。当您推送事件时,您应该在您的 JavaScript 控制台中看到消息。如果没有,请检查 auth/ 的 ajax 请求,因为您需要认证,因为您正在订阅私有频道。
尚未包含的功能
尚未处理存在频道的身份验证 获取频道信息 设置 socket_id 以避免重复事件...