soleon / sc-php
SocketCluster - PHP 库,用于与 SocketCluster.io 交互
v1.0.2
2016-07-27 15:19 UTC
Requires
- php: >=5.5.9
- textalk/websocket: 1.0.*
Requires (Dev)
- illuminate/broadcasting: 5.2.*
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~4.0
- pimple/pimple: ~3.0
Suggests
- laravel/framework: Framework Laravel (5.2.*)
This package is not auto-updated.
Last update: 2024-09-28 19:37:38 UTC
README
PHP 库,用于与 SocketCluster.io 交互
这是 SocketCluster(一个由 socketcluster.io 提供的开源实时 WebSocket 框架,适用于 PHP 5.5.9+)的非官方 PHP 客户端。
内容
安装
您可以通过运行以下 composer 命令来安装此 包:
composer require soleon/sc-php
基本用法
$optionsOrUri = 'wss://:443/socketcluster/?servicekey=abc' OR $optionsOrUri = [ 'secure' => true, 'host' => 'localhost', 'port' => '443', 'path' => '/socketcluster/', 'query' => [ 'servicekey' => 'abc' ], ]; $websocket = \SocketCluster\WebSocket::factory($optionsOrUri); $socket = new \SocketCluster\SocketCluster($websocket); // Event Emit $data = ['message' => 'FooBar']; $socket->publish('CHANNEL_NAME', $data);
集成
Laravel 框架
然后,在您的 providers 数组 [app/config/app.php]
中添加此服务提供程序
SocketCluster\Providers\LaravelServiceProvider::class,
然后,在您的 aliases 数组 [app/config/app.php]
中添加此 Facade
'SocketCluster' => SocketCluster\Laravel\SCFacade::class
接下来,您需要将配置复制到您的 connections
数组 [app/config/broadcasting.php]
/* * Set default broadcasting driver to socketcluster */ 'default' => env('BROADCAST_DRIVER', 'socketcluster'), 'socketcluster' => [ 'driver' => 'socketcluster', 'options' => [ 'secure' => true, 'host' => 'localhost', 'port' => '443', 'path' => '/socketcluster/', 'query' => [], ], ]
Laravel 用法
- 使用 Facade
SocketCluster::publish('ChannelName', ['message' => 'Test publish!!']);
使用事件监听器
例如,向您的应用程序添加一个自定义广播事件 [app/events/PublishToSocketClusterEvent.php]
namespace App\Events; use App\Events\Event; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class PublishToSocketClusterEvent implements ShouldBroadcast { use SerializesModels /** * Content Message * @var string */ public $message; /** * Construct Event * @param string $message */ public function __construct($message) { $this->message = $message; } /** * Get the channels the event should broadcast on. * @return array */ public function broadcastOn() { return ['channelName']; } /** * Get the data to send. * @return array */ public function broadcastWith() { return [ 'message' => $this->message ] } }
现在,在您的应用程序中发布事件
event(new App\Events\PublishToSocketClusterEvent('Test publish!!'));
Pimple
Pimple 是一个简单的 PHP 依赖注入容器
注册此服务提供程序
$app->register(new SocketCluster\Providers\PimpleServiceProvider(), array( 'socketcluster.options' => array( 'secure' => true, 'host' => 'localhost', 'port' => '443', 'path' => '/socketcluster/', 'query' => [], ) ));
Pimple 用法
$app['socketcluster']->publish('CHANNEL_NAME', $data);
贡献
支持 PSR-2 和 PSR-4 PHP 编码标准和语义版本。克隆此项目并提交一个 pull request!
许可证
本项目是免费软件,根据 MIT 许可证 进行分发。