|
2 | 2 | # Socket |
3 | 3 |
|
4 | 4 | ## This is a layer for client and server socket connections. |
5 | | -#### [Detailed documentation](http://socket-docs.epizy.com) |
| 5 | + |
| 6 | +### PHP extension which have to be installed before to work: |
| 7 | +* [PHP 8.0](https://www.php.net/downloads) |
| 8 | +* [Yaml](https://www.php.net/manual/en/book.yaml.php) |
| 9 | +* [Sockets](https://www.php.net/manual/en/book.sockets.php) |
| 10 | + |
| 11 | +### Four actions you have to make: |
| 12 | +* Configure your configuration file. |
| 13 | +* Create client.php or server.php file |
| 14 | +* Call methods |
| 15 | +* Run from CLI or extends |
| 16 | + |
| 17 | +### Examples of TCP and UNIX sockets: |
| 18 | + |
| 19 | +#### 1) Create configuration file. File can be **ONLY** with yaml(yml) or json extensions |
| 20 | + |
| 21 | +##### * TCP socket configuration using YAML |
| 22 | +``` |
| 23 | +settings: |
| 24 | + socket_type: tcp |
| 25 | + address: 127.0.0.1 |
| 26 | + port: 8000 |
| 27 | + content_length: 2048 |
| 28 | +``` |
| 29 | + |
| 30 | +##### * UNIX socket configuration using YAML |
| 31 | +``` |
| 32 | +settings: |
| 33 | + socket_type: unix |
| 34 | + address: socket.sock |
| 35 | + content_length: 2048 |
| 36 | +``` |
| 37 | + |
| 38 | +#### 2) Create client and server file handlers. |
| 39 | + |
| 40 | +##### server.php |
| 41 | +``` |
| 42 | +<?php |
| 43 | +
|
| 44 | +require_once 'vendor/autoload.php'; |
| 45 | +
|
| 46 | +use Qonsillium\QonsilliumSocket; |
| 47 | +use Qonsillium\ServerSocket; |
| 48 | +
|
| 49 | +$server = new QonsilliumSocket('config.yaml'); |
| 50 | +$server->runServer(function(ServerSocket $socket) { |
| 51 | + echo $socket->send('Hello from server!'); |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +##### client.php |
| 56 | +``` |
| 57 | +<?php |
| 58 | +
|
| 59 | +require_once 'vendor/autoload.php'; |
| 60 | +
|
| 61 | +use Qonsillium\QonsilliumSocket; |
| 62 | +use Qonsillium\ClientSocket; |
| 63 | +
|
| 64 | +$server = new QonsilliumSocket('config.yaml'); |
| 65 | +$server->runClient(function(ClinetSocket $socket) { |
| 66 | + echo $socket->send('Hello from client!'); |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +#### 3) Run from CLI |
| 71 | +``` |
| 72 | + john@doe:/workdir/$ php server.php |
| 73 | + john@doe:/workdir/$ php client.php |
| 74 | +``` |
| 75 | + |
| 76 | +#### 4) Extends from QonsilliumSocket |
| 77 | +``` |
| 78 | +<?php |
| 79 | +
|
| 80 | +namespace App; |
| 81 | +
|
| 82 | +use Qonsillium\QonsilliumSocket |
| 83 | +use Qonillium\ClientSocket |
| 84 | +
|
| 85 | +class SocketMessagePrinter extends QonsilliumSocket |
| 86 | +{ |
| 87 | + public function handleServerSocketMessage(string $myMessage) |
| 88 | + { |
| 89 | + $serverMessage = $this->runClient(function(ClientSocket $client) use ($myMessage) { |
| 90 | + return $client->send($myMessage); |
| 91 | + }); |
| 92 | +
|
| 93 | + if ($serverMessage === 'Hello from server!') { |
| 94 | + // handle this action |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +But when you will instantiate handler class don't forget to set configuration file with socket settings in constructor method |
0 commit comments