rdlowrey / nbsock
Async socket connection / server tools for Amp.
Fund package maintenance!
amphp
Requires
- php: >=8
- ext-openssl: *
- amphp/amp: ^3
- amphp/byte-stream: ^2
- amphp/dns: ^2
- kelunik/certificate: ^1.1
- league/uri: ^6.5
- revolt/event-loop: ^0.1.1
Requires (Dev)
- amphp/php-cs-fixer-config: dev-master
- amphp/phpunit-util: ^3
- phpunit/phpunit: ^9
- psalm/phar: ^4.7
- v2.x-dev
- v2.0.0-beta.1
- dev-master / 1.x-dev
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- v0.10.x-dev
- v0.10.13
- v0.10.12
- v0.10.11
- v0.10.10
- v0.10.9
- v0.10.8
- v0.10.7
- v0.10.6
- v0.10.5
- v0.10.4
- v0.10.3
- v0.10.2
- v0.10.1
- v0.10.0
- 0.9.x-dev
- v0.9.9
- v0.9.8
- v0.9.7
- v0.9.6
- v0.9.5
- v0.9.4
- v0.9.3
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.0
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.0
- dev-v2-revolt
- dev-socks5
- dev-refactor
- dev-remove-amphp-uri
- dev-byte-stream-issue-46
- dev-ext-async
- dev-ipv6-disable-if-not-bindable
- dev-ipv6
- dev-cert-md5-sha1
This package is not auto-updated.
Last update: 2022-02-01 12:40:03 UTC
README
amphp/socket
is a socket library for establishing and encrypting non-blocking sockets PHP based on Amp.
Installation
This package can be installed as a Composer dependency.
composer require amphp/socket
Documentation
Documentation can be found on amphp.org as well as in the ./docs
directory.
Examples
You can find more examples in the ./examples
directory.
Client Example
<?php // basic (and dumb) HTTP client require __DIR__ . '/../vendor/autoload.php'; // This is a very simple HTTP client that just prints the response without parsing. // league/uri-schemes required for this example. use Amp\ByteStream; use Amp\Loop; use Amp\Socket\ClientTlsContext; use Amp\Socket\ConnectContext; use Amp\Socket\EncryptableSocket; use League\Uri; use function Amp\Socket\connect; Loop::run(static function () use ($argv) { $stdout = ByteStream\getStdout(); if (\count($argv) !== 2) { yield $stdout->write('Usage: examples/simple-http-client.php <url>' . PHP_EOL); exit(1); } $uri = Uri\Http::createFromString($argv[1]); $host = $uri->getHost(); $port = $uri->getPort() ?? ($uri->getScheme() === 'https' ? 443 : 80); $path = $uri->getPath() ?: '/'; $connectContext = (new ConnectContext) ->withTlsContext(new ClientTlsContext($host)); /** @var EncryptableSocket $socket */ $socket = yield connect($host . ':' . $port, $connectContext); if ($uri->getScheme() === 'https') { yield $socket->setupTls(); } yield $socket->write("GET {$path} HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n"); while (null !== $chunk = yield $socket->read()) { yield $stdout->write($chunk); } // If the promise returned from `read()` resolves to `null`, the socket closed and we're done. // In this case you can also use `yield Amp\ByteStream\pipe($socket, $stdout)` instead, // but we want to demonstrate the `read()` method here. });
Server Example
<?php // basic (and dumb) HTTP server require __DIR__ . '/../vendor/autoload.php'; // This is a very simple HTTP server that just prints a message to each client that connects. // It doesn't check whether the client sent an HTTP request. // You might notice that your browser opens several connections instead of just one, // even when only making one request. use Amp\Loop; use Amp\Socket\ResourceSocket; use Amp\Socket\Server; use function Amp\asyncCoroutine; Loop::run(static function () { $clientHandler = asyncCoroutine(static function (ResourceSocket $socket) { $address = $socket->getRemoteAddress(); $ip = $address->getHost(); $port = $address->getPort(); echo "Accepted connection from {$address}." . PHP_EOL; $body = "Hey, your IP is {$ip} and your local port used is {$port}."; $bodyLength = \strlen($body); $req = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: {$bodyLength}\r\n\r\n{$body}"; yield $socket->end($req); }); $server = Server::listen('127.0.0.1:0'); echo 'Listening for new connections on ' . $server->getAddress() . ' ...' . PHP_EOL; echo 'Open your browser and visit http://' . $server->getAddress() . '/' . PHP_EOL; while ($socket = yield $server->accept()) { $clientHandler($socket); } });
Security
If you discover any security related issues, please email me@kelunik.com
instead of using the issue tracker.
License
The MIT License (MIT). Please see LICENSE
for more information.