-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vasilevrv
authored
Mar 22, 2018
1 parent
a301674
commit 6093773
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,92 @@ | ||
# tproto | ||
|
||
### How to use. | ||
|
||
Create Transmitter. For example, use transmitter with unbuffered PDO query and symfony: | ||
|
||
``` | ||
return new \Symfony\Component\HttpFoundation\StreamedResponse(function() use ($pdo) { | ||
$pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); | ||
$res = $pdo->query("SELECT id, status FROM tblclients"); | ||
while (false !== ($row = $res->fetch(\PDO::FETCH_ASSOC))) { | ||
$tm->send($row); | ||
} | ||
}); | ||
``` | ||
|
||
Create receiver. For example (now there is only guzzle adapter, you can use your own adapters): | ||
|
||
``` | ||
$stream = $client->get('https://test.com/stream')->getBody(); | ||
$update = new \RV\TProto\Proto\Receiver\Receiver($stream, function($data) { | ||
print_r($data); | ||
}); | ||
$update->run(); | ||
``` | ||
|
||
If you need to receive objects not one at a time, but a batch, then you can use BatchReceiver (200 in batch, for example): | ||
|
||
``` | ||
$stream = $client->get('https://test.com/stream')->getBody(); | ||
$update = new \RV\TProto\Proto\Receiver\BatchReceiver($stream, function($data) { | ||
print count($data); | ||
}, null, 200); | ||
$update->run(); | ||
``` | ||
|
||
--- | ||
|
||
Also, you can use customize serializer and DTO: | ||
|
||
``` | ||
class Client | ||
{ | ||
public $id; | ||
public $status; | ||
public function __construct($id, $status) | ||
{ | ||
$this->id = $id; | ||
$this->status = $status; | ||
} | ||
} | ||
``` | ||
|
||
``` | ||
class CustomSerializer implements \RV\TProto\Serializer\SerializerInterface | ||
{ | ||
public function serialize($data) | ||
{ | ||
return $data['id'].";".$data['status']; | ||
} | ||
public function deserialize($data) | ||
{ | ||
return new Client($data['id'], $data['status']); | ||
} | ||
public function getDelimiter() | ||
{ | ||
return ":"; | ||
} | ||
} | ||
``` | ||
|
||
And use it on transmitter and receiver, for example receiver: | ||
|
||
``` | ||
$update = new \RV\TProto\Proto\Receiver\Receiver($stream, function($data) { | ||
var_dump($data); | ||
}, new CustomSerializer()); | ||
``` | ||
|
||
It print object: | ||
|
||
``` | ||
object(Client)#1 (2) { | ||
["id"]=> | ||
int(123) | ||
["status"]=> | ||
int(0) | ||
} | ||
``` |