Skip to content

Commit

Permalink
GuzzleHttp removed, global update
Browse files Browse the repository at this point in the history
  • Loading branch information
skoniks authored and skoniks committed Nov 2, 2018
1 parent 62a5034 commit 6b577ce
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 198 deletions.
Binary file added .DS_Store
Binary file not shown.
22 changes: 0 additions & 22 deletions LICENSE

This file was deleted.

77 changes: 34 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# skoniks / php_cent
Centrifugo (Centrifuge) [1.6+] PHP Server REDIS & HTTP API implementation for Laravel 5+

Centrifugo (Centrifuge) [1.0+] PHP Server REDIS & HTTP API implementation for Laravel 5+
Incompatible with Centrifugo [2.0+], will be updated later!
## Base Installation
1. Run `composer require skoniks/php_cent` & `composer update`
2. Create `config/centrifugo.php` as provided below
Expand All @@ -9,17 +9,7 @@ Centrifugo (Centrifuge) [1.6+] PHP Server REDIS & HTTP API implementation for La
>For laravel 5.5+ use version >= "2.5"
## Config example `config/centrifugo.php`
```php
<?php
return [
'driver' => 'centrifugo', // redis channel name as provided in cent. conf ($driver.".api")
'transport' => 'http', // http || redis connection, check more information below
'redisConnection' => 'centrifugo', // only for redis, name of connection more information below
'baseUrl' => 'http://localhost:8000/api/', // full api url
'secret' => 'shlasahaposaheisasalssushku', // you super secret key
];

```
[centrifugo.php](https://github.com/skoniks/php_cent/blob/master/centrifugo.php)

## Alias additions `config/app.php`
```php
Expand All @@ -31,27 +21,25 @@ Centrifugo (Centrifuge) [1.6+] PHP Server REDIS & HTTP API implementation for La
```

## Setting redis as transport
>Read notes about redis transport provided methods below. To set redis as transport :
1. Add your redis connections add your connection to `config/database.php` as provided below
1. Add your redis connection to `config/database.php`
2. Change `config/centrifugo.php` to redis settings

## Adding redis connection `config/database.php`
```php
'redis' => [
...
'redis' => [
...
'centrifugo' => [
'scheme' => 'tcp', // unix
'host' => '127.0.0.1', // null for unix
'path' => '', // or unix path
'scheme' => 'tcp',
'host' => '127.0.0.1',
'password' => '',
'port' => 6379, // null for unix
'database' => 1, // cent. db like in cent. configs
'port' => 6379,
'database' => 1,
],
],
...
```


## Redis supported transport
>Make shure that **HTTP connection must work independently from redis connection**.
>It is because redis transport provides only this methods:
Expand All @@ -60,39 +48,42 @@ Centrifugo (Centrifuge) [1.6+] PHP Server REDIS & HTTP API implementation for La
* 'unsubscribe'
* 'disconnect'

>Redis dont provides this methods:
>Redis dont provide this methods:
* presence
* history
* channels
* stats
* node

## [Module usage || sending your requests] example
```php
<?php
use Centrifugo;

class Controller
{
public function your_func()
{
class Controller {
public function _function(){
// declare Centrifugo
$Centrifugo = new Centrifugo();
$centrifugo = new Centrifugo();

// generating token example
$current_time = time();
$steamid = '76561198073063637'
$token = $Centrifugo->generateToken($steamid, $current_time, '');
$user_id = '1234567890';
$token = Centrifugo::token($user_id, $current_time, 'custom info');

// publishing example
$Centrifugo->publish("channel" , ["yout text or even what rou want"]);
$centrifugo->publish("channel" , ["custom data"]);

// each method returns its response;
// list of awailible methods:
$response = $Centrifugo->publish($channle, $messageData);
$response = $Centrifugo->unsubscribe($channle, $userId);
$response = $Centrifugo->disconnect($userId);
$response = $Centrifugo->presence($channle);
$response = $Centrifugo->history($channle);
$response = $Centrifugo->generateToken($user, $timestamp, $info);

// You can create a controller to bild your own interface;
$response = $centrifugo->publish($channel, $data);
$response = $centrifugo->unsubscribe($channel, $user_id);
$response = $centrifugo->disconnect($user_id);
$response = $centrifugo->presence($channel);
$response = $centrifugo->history($channel);
$response = $centrifugo->channels();
$response = $centrifugo->stats();
$response = $centrifugo->node();
$token = Centrifugo::token($user_id, $timestamp, $info);

// $response == false | when error
}
```
### For more informations go [here](https://fzambia.gitbooks.io/centrifugal/content/)
### For more information go [here](https://fzambia.gitbooks.io/centrifugal/content/)
10 changes: 10 additions & 0 deletions centrifugo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
return [
'redis' => [
'enable' => false, // Use REDIS for redis-available methods (by default uses http)
'driver' => 'centrifugo' // REDIS channel name from Centrifugo config ($driver.".api")
'connection' => 'centrifugo' // Name of REDIS connection in "config/database.php"
],
'url_api' => env('C_URL_API', 'http://localhost:8000/api/'), // HTTP API url for Centrifugo
'secret' => env('C_SECRET', null), // SUPER SECRET API KEY
];
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "skoniks/php_cent",
"description": "Centrifugo (Centrifuge) [1.6+] PHP Server REDIS & HTTP API implementation for Laravel 5+",
"description": "Centrifugo (Centrifuge) [1.0+] PHP Server REDIS & HTTP API implementation for Laravel 5+",
"keywords": ["laravel", "centrifuge", "centrifugo", "php"],
"license": "MIT",
"authors": [
Expand All @@ -10,10 +10,7 @@
}
],
"require": {
"illuminate/container": "5.5.*",
"illuminate/support": "5.5.*",
"predis/predis": "^1.1",
"guzzlehttp/guzzle": "6.*"
"illuminate/container": "5.*",
},
"autoload": {
"psr-4": {
Expand Down
71 changes: 36 additions & 35 deletions src/Centrifugo.php
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
<?php
namespace SKONIKS\Centrifugo;
use SKONIKS\Centrifugo\Transport\RedisClient;
use SKONIKS\Centrifugo\Transport\HttpClient;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Redis;
use SKONIKS\Centrifugo\Transport\CHttp;
use SKONIKS\Centrifugo\Transport\CRedis;

class Centrifugo
{
protected $rmethods = ['publish', 'broadcast', 'unsubscribe', 'disconnect'];
public function publish($channel, $data)
{
class Centrifugo {
protected $redis_methods = [
'publish', 'broadcast', 'unsubscribe', 'disconnect'
];
public function publish($channel, $data){
return $this->send('publish', [
'channel' => $channel,
'data' => $data,
]);
}
public function unsubscribe($channel, $user)
{
public function broadcast((array)$channels, $data){
return $this->send('broadcast', [
'channels' => $channels,
'data' => $data,
]);
}
public function unsubscribe($channel, $user){
return $this->send('unsubscribe', [
'channel' => $channel,
'user' => $user,
]);
}
public function disconnect($user)
{
public function disconnect($user){
return $this->send('disconnect', [
'user' => $user,
]);
}
public function presence($channel)
{
public function presence($channel){
return $this->send('presence', [
'channel' => $channel,
]);
}
public function history($channel)
{
public function history($channel){
return $this->send('history', [
'channel' => $channel,
]);
}
public function generateToken($userId, $timestamp, $info = '')
{
public function channels(){
return $this->send('channels', []);
}
public function stats(){
return $this->send('stats', []);
}
public function node(){
return $this->send('node', []);
}
protected function transport($method){
if(config('centrifugo.redis.enable') && in_array($method, $this->redis_methods))
return new RedisClient();
return new HttpClient();
}
protected function send($method, $params){
return $this->transport($method)->send($method, $params);
}
public static function token($user_id, $timestamp, $info = ''){
$ctx = hash_init('sha256', HASH_HMAC, config('centrifugo.secret'));
hash_update($ctx, $userId);
hash_update($ctx, $user_id);
hash_update($ctx, $timestamp);
hash_update($ctx, $info);
return hash_final($ctx);
}
protected function getTransport($method){
if(config('centrifugo.transport') == 'redis' && in_array($method, $this->rmethods)) {
$client = Redis::connection(config('centrifugo.redisConnection'))->client();
return new CRedis($client, config('centrifugo.driver'));
} else {
$client = new Client(['base_uri' => config('centrifugo.baseUrl')]);
return new CHttp($client);
}
}
protected function send($method, $params){
$transport = $this->getTransport($method);
$response = $transport->send($method, $params);
return $response;
}
}
8 changes: 0 additions & 8 deletions src/Exceptions/CentrifugoException.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exceptions/HttpException.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exceptions/RedisException.php

This file was deleted.

41 changes: 0 additions & 41 deletions src/Transport/CHttp.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/Transport/CRedis.php

This file was deleted.

Loading

0 comments on commit 6b577ce

Please sign in to comment.