|
1 | | -# Socket |
| 1 | +# InitPHP Socket Manager |
| 2 | + |
| 3 | +PHP Socket (TCP, TLS, UDP, SSL) Server/Client Library |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- PHP 7.4 or higher |
| 8 | +- PHP Sockets Extension |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +``` |
| 13 | +composer require initphp/socket |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +**Supported Types :** |
| 19 | + |
| 20 | +- TCP |
| 21 | +- UDP |
| 22 | +- TLS |
| 23 | +- SSL |
| 24 | + |
| 25 | +### Factory |
| 26 | + |
| 27 | +`\InitPHP\Socket\Socket::class` It allows you to easily create socket server or client. |
| 28 | + |
| 29 | +#### `Socket::server()` |
| 30 | + |
| 31 | +```php |
| 32 | +public static function server(int $handler = Socket::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketServerInterface |
| 33 | +``` |
| 34 | + |
| 35 | +- `$handler` : `Socket::SSL`, `Socket::TCP`, `Socket::TLS` or `Socket::UDP` |
| 36 | +- `$host` : Identifies the socket host. If not defined or left blank, it will throw an error. |
| 37 | +- `$port` : Identifies the socket port. If not defined or left blank, it will throw an error. |
| 38 | +- `$argument` : This value is the value that will be sent as 3 parameters to the constructor method of the handler. |
| 39 | + - SSL or TLS = (float) Defines the timeout period. |
| 40 | + - UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix" |
| 41 | + |
| 42 | +#### `Socket::client()` |
| 43 | + |
| 44 | +```php |
| 45 | +public static function client(int $handler = self::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketClientInterface |
| 46 | +``` |
| 47 | + |
| 48 | +- `$handler` : `Socket::SSL`, `Socket::TCP`, `Socket::TLS` or `Socket::UDP` |
| 49 | +- `$host` : Identifies the socket host. If not defined or left blank, it will throw an error. |
| 50 | +- `$port` : Identifies the socket port. If not defined or left blank, it will throw an error. |
| 51 | +- `$argument` : This value is the value that will be sent as 3 parameters to the constructor method of the handler. |
| 52 | + - SSL or TLS = (float) Defines the timeout period. |
| 53 | + - UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix" |
| 54 | + |
| 55 | +### Methods |
| 56 | + |
| 57 | +**`connection()` :** Initiates the socket connection. |
| 58 | + |
| 59 | +```php |
| 60 | +public function connection(): self; |
| 61 | +``` |
| 62 | + |
| 63 | +**`disconnect()` :** Terminates the connection. |
| 64 | + |
| 65 | +```php |
| 66 | +public function disconnect(): bool; |
| 67 | +``` |
| 68 | + |
| 69 | +**`read()` :** Reads data from socket. |
| 70 | + |
| 71 | +```php |
| 72 | +public function read(int $length = 1024): ?string; |
| 73 | +``` |
| 74 | + |
| 75 | +**`write()` :** Writes data to the socket |
| 76 | + |
| 77 | +```php |
| 78 | +public function write(string $string): ?int; |
| 79 | +``` |
| 80 | + |
| 81 | +#### Server Methods |
| 82 | + |
| 83 | +**`live()` :** |
| 84 | + |
| 85 | +```php |
| 86 | +public function live(callable $callback): void; |
| 87 | +``` |
| 88 | + |
| 89 | +**`wait()` :** |
| 90 | + |
| 91 | +```php |
| 92 | +public function wait(int $second): void; |
| 93 | +``` |
| 94 | + |
| 95 | +#### Special methods for TLS and SSL. |
| 96 | + |
| 97 | +TLS and SSL work similarly. |
| 98 | + |
| 99 | +There are some additional methods you can use from TLS and SSL sockets. |
| 100 | + |
| 101 | +**`timeout()` :** Defines the timeout period of the current. |
| 102 | + |
| 103 | +```php |
| 104 | +public function timeout(int $second): self; |
| 105 | +``` |
| 106 | + |
| 107 | +**`blocking()` :** Sets the blocking mode of the current. |
| 108 | + |
| 109 | +```php |
| 110 | +public function blocking(bool $mode = true): self; |
| 111 | +``` |
| 112 | + |
| 113 | +**`crypto()` :** Turns encryption on or off on a connected socket. |
| 114 | + |
| 115 | +```php |
| 116 | +public function crypto(?string $method = null): self; |
| 117 | +``` |
| 118 | + |
| 119 | +Possible values for `$method` are; |
| 120 | + |
| 121 | +- "sslv2" |
| 122 | +- "sslv3" |
| 123 | +- "sslv23" |
| 124 | +- "any" |
| 125 | +- "tls" |
| 126 | +- "tlsv1.0" |
| 127 | +- "tlsv1.1" |
| 128 | +- "tlsv1.2" |
| 129 | +- NULL |
| 130 | + |
| 131 | +**`option()` :** Defines connection options for SSL and TLS. see; [https://www.php.net/manual/en/context.ssl.php](https://www.php.net/manual/en/context.ssl.php) |
| 132 | + |
| 133 | +```php |
| 134 | +public function option(string $key, mixed $value): self; |
| 135 | +``` |
| 136 | + |
| 137 | +### Socket Server |
| 138 | + |
| 139 | +_**Example :**_ |
| 140 | + |
| 141 | +```php |
| 142 | +require_once "../vendor/autoload.php"; |
| 143 | +use \InitPHP\Socket\Socket; |
| 144 | +use \InitPHP\Socket\Interfaces\SocketServerInterface; |
| 145 | + |
| 146 | +$server = Socket::server(Socket::TLS, '127.0.0.1', 8080); |
| 147 | +$server->connection(); |
| 148 | + |
| 149 | +$server->live(function (SocketServerInterface $socket) { |
| 150 | + switch ($socket->read()) { |
| 151 | + case 'exit' : |
| 152 | + $socket->write('Goodbye!'); |
| 153 | + return; |
| 154 | + case 'write' : |
| 155 | + $socket->write('Run write command.'); |
| 156 | + break; |
| 157 | + case 'read' : |
| 158 | + $socket->write('Run read command.'); |
| 159 | + break; |
| 160 | + default: return; |
| 161 | + } |
| 162 | +}); |
| 163 | +``` |
| 164 | + |
| 165 | +### Socket Client |
| 166 | + |
| 167 | +_**Example :**_ |
| 168 | + |
| 169 | +```php |
| 170 | +require_once "../vendor/autoload.php"; |
| 171 | +use \InitPHP\Socket\Socket; |
| 172 | + |
| 173 | +$client = Socket::client(Socket::SSL, 'smtp.gmail.com', 465); |
| 174 | + |
| 175 | +$client->option('verify_peer', false) |
| 176 | + ->option('verify_peer_name', false); |
| 177 | + |
| 178 | +$client->connection(); |
| 179 | + |
| 180 | +$client->write('EHLO [127.0.0.1]'); |
| 181 | + |
| 182 | +echo $client->read(); |
| 183 | +``` |
| 184 | + |
| 185 | +_In the above example, a simple smtp connection to gmail is made._ |
| 186 | + |
| 187 | +## Credits |
| 188 | + |
| 189 | +- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<info@muhammetsafak.com.tr>> |
| 190 | + |
| 191 | +## License |
| 192 | + |
| 193 | +Copyright © 2022 [MIT License](./LICENSE) |
0 commit comments