Skip to content

Commit 6b577ce

Browse files
skoniksskoniks
authored andcommitted
GuzzleHttp removed, global update
1 parent 62a5034 commit 6b577ce

File tree

13 files changed

+130
-198
lines changed

13 files changed

+130
-198
lines changed

.DS_Store

6 KB
Binary file not shown.

LICENSE

Lines changed: 0 additions & 22 deletions
This file was deleted.

README.md

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# skoniks / php_cent
2-
Centrifugo (Centrifuge) [1.6+] PHP Server REDIS & HTTP API implementation for Laravel 5+
3-
2+
Centrifugo (Centrifuge) [1.0+] PHP Server REDIS & HTTP API implementation for Laravel 5+
3+
Incompatible with Centrifugo [2.0+], will be updated later!
44
## Base Installation
55
1. Run `composer require skoniks/php_cent` & `composer update`
66
2. Create `config/centrifugo.php` as provided below
@@ -9,17 +9,7 @@ Centrifugo (Centrifuge) [1.6+] PHP Server REDIS & HTTP API implementation for La
99
>For laravel 5.5+ use version >= "2.5"
1010
1111
## Config example `config/centrifugo.php`
12-
```php
13-
<?php
14-
return [
15-
'driver' => 'centrifugo', // redis channel name as provided in cent. conf ($driver.".api")
16-
'transport' => 'http', // http || redis connection, check more information below
17-
'redisConnection' => 'centrifugo', // only for redis, name of connection more information below
18-
'baseUrl' => 'http://localhost:8000/api/', // full api url
19-
'secret' => 'shlasahaposaheisasalssushku', // you super secret key
20-
];
21-
22-
```
12+
[centrifugo.php](https://github.com/skoniks/php_cent/blob/master/centrifugo.php)
2313

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

3323
## Setting redis as transport
34-
>Read notes about redis transport provided methods below. To set redis as transport :
35-
36-
1. Add your redis connections add your connection to `config/database.php` as provided below
24+
1. Add your redis connection to `config/database.php`
3725
2. Change `config/centrifugo.php` to redis settings
3826

3927
## Adding redis connection `config/database.php`
4028
```php
41-
'redis' => [
29+
...
30+
'redis' => [
4231
...
4332
'centrifugo' => [
44-
'scheme' => 'tcp', // unix
45-
'host' => '127.0.0.1', // null for unix
46-
'path' => '', // or unix path
33+
'scheme' => 'tcp',
34+
'host' => '127.0.0.1',
4735
'password' => '',
48-
'port' => 6379, // null for unix
49-
'database' => 1, // cent. db like in cent. configs
36+
'port' => 6379,
37+
'database' => 1,
5038
],
5139
],
40+
...
5241
```
5342

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

63-
>Redis dont provides this methods:
51+
>Redis dont provide this methods:
6452
* presence
6553
* history
54+
* channels
55+
* stats
56+
* node
6657

6758
## [Module usage || sending your requests] example
6859
```php
6960
<?php
7061
use Centrifugo;
71-
72-
class Controller
73-
{
74-
public function your_func()
75-
{
62+
class Controller {
63+
public function _function(){
7664
// declare Centrifugo
77-
$Centrifugo = new Centrifugo();
65+
$centrifugo = new Centrifugo();
7866

7967
// generating token example
8068
$current_time = time();
81-
$steamid = '76561198073063637'
82-
$token = $Centrifugo->generateToken($steamid, $current_time, '');
69+
$user_id = '1234567890';
70+
$token = Centrifugo::token($user_id, $current_time, 'custom info');
71+
8372
// publishing example
84-
$Centrifugo->publish("channel" , ["yout text or even what rou want"]);
73+
$centrifugo->publish("channel" , ["custom data"]);
8574

86-
// each method returns its response;
8775
// list of awailible methods:
88-
$response = $Centrifugo->publish($channle, $messageData);
89-
$response = $Centrifugo->unsubscribe($channle, $userId);
90-
$response = $Centrifugo->disconnect($userId);
91-
$response = $Centrifugo->presence($channle);
92-
$response = $Centrifugo->history($channle);
93-
$response = $Centrifugo->generateToken($user, $timestamp, $info);
94-
95-
// You can create a controller to bild your own interface;
76+
$response = $centrifugo->publish($channel, $data);
77+
$response = $centrifugo->unsubscribe($channel, $user_id);
78+
$response = $centrifugo->disconnect($user_id);
79+
$response = $centrifugo->presence($channel);
80+
$response = $centrifugo->history($channel);
81+
$response = $centrifugo->channels();
82+
$response = $centrifugo->stats();
83+
$response = $centrifugo->node();
84+
$token = Centrifugo::token($user_id, $timestamp, $info);
85+
86+
// $response == false | when error
9687
}
9788
```
98-
### For more informations go [here](https://fzambia.gitbooks.io/centrifugal/content/)
89+
### For more information go [here](https://fzambia.gitbooks.io/centrifugal/content/)

centrifugo.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
return [
3+
'redis' => [
4+
'enable' => false, // Use REDIS for redis-available methods (by default uses http)
5+
'driver' => 'centrifugo' // REDIS channel name from Centrifugo config ($driver.".api")
6+
'connection' => 'centrifugo' // Name of REDIS connection in "config/database.php"
7+
],
8+
'url_api' => env('C_URL_API', 'http://localhost:8000/api/'), // HTTP API url for Centrifugo
9+
'secret' => env('C_SECRET', null), // SUPER SECRET API KEY
10+
];

composer.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "skoniks/php_cent",
3-
"description": "Centrifugo (Centrifuge) [1.6+] PHP Server REDIS & HTTP API implementation for Laravel 5+",
3+
"description": "Centrifugo (Centrifuge) [1.0+] PHP Server REDIS & HTTP API implementation for Laravel 5+",
44
"keywords": ["laravel", "centrifuge", "centrifugo", "php"],
55
"license": "MIT",
66
"authors": [
@@ -10,10 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"illuminate/container": "5.5.*",
14-
"illuminate/support": "5.5.*",
15-
"predis/predis": "^1.1",
16-
"guzzlehttp/guzzle": "6.*"
13+
"illuminate/container": "5.*",
1714
},
1815
"autoload": {
1916
"psr-4": {

src/Centrifugo.php

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,67 @@
11
<?php
22
namespace SKONIKS\Centrifugo;
3+
use SKONIKS\Centrifugo\Transport\RedisClient;
4+
use SKONIKS\Centrifugo\Transport\HttpClient;
35

4-
use GuzzleHttp\Client;
5-
use Illuminate\Support\Facades\Redis;
6-
use SKONIKS\Centrifugo\Transport\CHttp;
7-
use SKONIKS\Centrifugo\Transport\CRedis;
8-
9-
class Centrifugo
10-
{
11-
protected $rmethods = ['publish', 'broadcast', 'unsubscribe', 'disconnect'];
12-
public function publish($channel, $data)
13-
{
6+
class Centrifugo {
7+
protected $redis_methods = [
8+
'publish', 'broadcast', 'unsubscribe', 'disconnect'
9+
];
10+
public function publish($channel, $data){
1411
return $this->send('publish', [
1512
'channel' => $channel,
1613
'data' => $data,
1714
]);
1815
}
19-
public function unsubscribe($channel, $user)
20-
{
16+
public function broadcast((array)$channels, $data){
17+
return $this->send('broadcast', [
18+
'channels' => $channels,
19+
'data' => $data,
20+
]);
21+
}
22+
public function unsubscribe($channel, $user){
2123
return $this->send('unsubscribe', [
2224
'channel' => $channel,
2325
'user' => $user,
2426
]);
2527
}
26-
public function disconnect($user)
27-
{
28+
public function disconnect($user){
2829
return $this->send('disconnect', [
2930
'user' => $user,
3031
]);
3132
}
32-
public function presence($channel)
33-
{
33+
public function presence($channel){
3434
return $this->send('presence', [
3535
'channel' => $channel,
3636
]);
3737
}
38-
public function history($channel)
39-
{
38+
public function history($channel){
4039
return $this->send('history', [
4140
'channel' => $channel,
4241
]);
4342
}
44-
public function generateToken($userId, $timestamp, $info = '')
45-
{
43+
public function channels(){
44+
return $this->send('channels', []);
45+
}
46+
public function stats(){
47+
return $this->send('stats', []);
48+
}
49+
public function node(){
50+
return $this->send('node', []);
51+
}
52+
protected function transport($method){
53+
if(config('centrifugo.redis.enable') && in_array($method, $this->redis_methods))
54+
return new RedisClient();
55+
return new HttpClient();
56+
}
57+
protected function send($method, $params){
58+
return $this->transport($method)->send($method, $params);
59+
}
60+
public static function token($user_id, $timestamp, $info = ''){
4661
$ctx = hash_init('sha256', HASH_HMAC, config('centrifugo.secret'));
47-
hash_update($ctx, $userId);
62+
hash_update($ctx, $user_id);
4863
hash_update($ctx, $timestamp);
4964
hash_update($ctx, $info);
5065
return hash_final($ctx);
5166
}
52-
protected function getTransport($method){
53-
if(config('centrifugo.transport') == 'redis' && in_array($method, $this->rmethods)) {
54-
$client = Redis::connection(config('centrifugo.redisConnection'))->client();
55-
return new CRedis($client, config('centrifugo.driver'));
56-
} else {
57-
$client = new Client(['base_uri' => config('centrifugo.baseUrl')]);
58-
return new CHttp($client);
59-
}
60-
}
61-
protected function send($method, $params){
62-
$transport = $this->getTransport($method);
63-
$response = $transport->send($method, $params);
64-
return $response;
65-
}
6667
}

src/Exceptions/CentrifugoException.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Exceptions/HttpException.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Exceptions/RedisException.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Transport/CHttp.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)