blenderdeluxe / ortc-laravel
基于 Laravel 框架的 ORTC (来自 realtime.co 的实时框架) API 封装器
1.5
2017-12-11 06:11 UTC
Requires
- php: >=5.5.9
- blenderdeluxe/ortc-php: 1.5
- illuminate/support: 5.1.* || 5.2.* || 5.3.* || 5.4.*
README
Laravel 框架 (Laravel 5.1 和 5.2) 上易于使用的 ORTC API 客户端
如果你使用的是 Laravel 4.2.x,请检查 分支 l4
此包基于 nikapps/ortc-php。
安装
您可以通过运行以下 composer 命令安装此 包
composer require nikapps/ortc-laravel
然后,将此服务提供者在您的 providers 数组中 [app/config/app.php]
Nikapps\OrtcLaravel\OrtcLaravelServiceProvider::class,
然后,将此 Facade 添加到您的 aliases 数组 [app/config/app.php]
'Ortc' => Nikapps\OrtcLaravel\Facades\Ortc::class
接下来,您需要将配置复制到您的 connections
数组 [app/config/broadcasting.php]
'realtime' => [ 'driver' => 'realtime', 'credentials' => [ /* * your application key */ 'application_key' => 'YOUR_APPLICATION_KEY', /* * your private key */ 'private_key' => 'YOUR_PRIVATE_KEY', ], /* |-------------------------------------------------------------------------- | Real-time REST API Options |-------------------------------------------------------------------------- | you can change default options of api. | */ 'api' => [ /* * send message */ 'send_message' => [ 'path' => '/send', //api path 'max_chunk_size' => 700, //maximum size of each message in bytes 'batch_pool_size' => 5, //pool size for concurrent requests 'pre_message_string' => '{RANDOM}_{PART}-{TOTAL_PARTS}_' //pre message string format ], /* * authentication */ 'authentication' => [ 'path' => '/authenticate' //api path ], /* * url to fetch balancer url */ 'balancer_url' => 'https://ortc-developers.realtime.co/server/2.1?appkey={APP_KEY}', /* * verify ssl/tls certificate */ 'verify_ssl' => true ] ]
配置
获取应用程序密钥和私钥
首先,您应该在 realtime.co 上注册并获取您的 API 密钥。
-
在 https://accounts.realtime.co 登录/注册
-
创建新的订阅
-
您可以看到您的
应用程序密钥
和私钥
-
如果您想使用身份验证,您应该在面板中启用它。
更新配置文件
编辑 app/config/broadcasting.php
/* * set default broadcasting driver to realtime */ 'default' => env('BROADCAST_DRIVER', 'realtime'), 'credentials' => [ /* * your application key */ 'application_key' => 'YOUR_APPLICATION_KEY', /* * your private key */ 'private_key' => 'YOUR_PRIVATE_KEY', ],
完成!
使用方法
手动获取均衡器 URL
此包自动获取均衡器 URL(最佳可用服务器),但如果您想手动获取新的均衡器 URL
$balancerUrl = Ortc::getBalancerUrl(); echo 'Balancer Url: ' . $balancerUrl->getUrl();
身份验证
为了验证用户
$channels = [ 'channel_one' => 'w', 'channel_two' => 'r' ]; Ortc::authenticate( $authToken, //authentication token $channels, $ttl, //(optional) default: 3600 $isPrivate //(optional) default: false );
通过广播服务发送消息(推荐)(推送)
以下是一个简短的示例
创建一个新的事件
php artisan make:event TestEvent
打开 app/Events/TestEvent.php 并编辑
<?php namespace App\Events; use App\Events\Event; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; /** * Add implements ShouldBroadcast to EventClass */ class TestEvent extends Event implements ShouldBroadcast { use SerializesModels; /** * All public variables will be automatically added to the broadcast payload. */ public $value; private $userId; /** * Create a new event instance. * * @param User $user * @param $value * @return void */ public function __construct(User $user, $value) { $this->userId = $user->id; $this->value = $value; } /** * Get the channels the event should be broadcast on. * * @return array */ public function broadcastOn() { return ['userId_' . $userId]; } /** * Get the broadcast event name. * * @return mixed also could be an array */ public function broadcastAs() { return 'testEvent'; } }
触发事件
$value = 'test 123'; event(new TestEvent($user, $value));
结果将是
{ "event": "testEvent", "payload": { "value": "test 123" } }
手动发送消息(推送)
为了向频道推送消息
Ortc::send( $channel, //channel name $authToken, //authentication token $message //message (string) );
如果您使用的是 UTF-8 消息,最好使用 base64_encode()
。
异常
依赖关系
Ortc 文档
此包基于 ORTC REST API。您可以从此 URL 下载 REST 服务文档
http://messaging-public.realtime.co/documentation/rest/2.1.0/RestServices.pdf
待办事项
- 添加单元测试(codeception 或 phpunit)
- 通过 Ratchet/Nodejs 订阅频道
- 支持移动推送通知(iOS 和 Android)
- 支持存在频道
- 还有其他什么吗?!
鸣谢
- 感谢 realtime.co 团队提供的出色平台
- 感谢 João Parreira 提供的 PHP 库
- 感谢 rdarda 提交拉取请求以支持 Laravel 5.1
贡献
想贡献?只需分叉此项目并提交拉取请求!
许可
本项目采用 MIT 许可证 发布。
/*
* Copyright (C) 2015 NikApps Team.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* 1- The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* 2- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/