nikapps / ortc-laravel
基于 Laravel 框架的 ORTC (来自realtime.co的实时框架) API 包装器
Requires
- php: >=5.5.9
- illuminate/support: 5.1.* || 5.2.*
- nikapps/ortc-php: 1.*
This package is not auto-updated.
Last update: 2024-09-14 17:11:07 UTC
README
Laravel 框架 (Laravel 5.1 和 5.2) 的简单易用 ORTC API 客户端
如果你使用的是 Laravel 4.2.x,请检查 分支 l4
此包基于 nikapps/ortc-php。
安装
你可以通过运行以下 composer 命令安装此 包
composer require nikapps/ortc-laravel
然后,在 [app/config/app.php]
的 providers 数组中添加此服务提供者
Nikapps\OrtcLaravel\OrtcLaravelServiceProvider::class,
然后,在 [app/config/app.php]
的 aliases 数组中添加此 Facade
'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。你可以从以下链接下载 REST 服务文档
http://messaging-public.realtime.co/documentation/rest/2.1.0/RestServices.pdf
TODO
- 添加单元测试 (codeception 或 phpunit)
- 通过 Ratchet/Nodejs 订阅频道
- 支持移动推送通知(iOS & Android)
- 支持存在频道
- 还有其他吗?!
致谢
- 感谢realtime.co团队提供的出色平台
- 感谢 João Parreira 提供的 PHP 库
- 感谢 rdarda 提交的拉取请求以支持 Laravel 5.1
贡献
想贡献吗?只需fork此项目并提交拉取请求!
许可
此项目根据 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.
*/