2017 © Pedro Peláez
 

library socket

Wrapper to PHP socket functions

image

innmind/socket

Wrapper to PHP socket functions

  • Wednesday, October 25, 2017
  • by Baptouuuu
  • Repository
  • 1 Watchers
  • 0 Stars
  • 328 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 5 % Grown

The README.md

Socket

Build Status codecov Type Coverage, (*1)

Layer on top of innmind/stream to specifically work with sockets., (*2)

Installation

composer require innmind/socket

Usage

Unix socket

Server example:, (*3)

use Innmind\Socket\{
    Server\Unix,
    Address\Unix as Address,
};
use Innmind\TimeContinuum\Earth\ElapsedPeriod;
use Innmind\Stream\Watch\Select;

$server = Unix::recoverable(Address::of('/tmp/my-socket'))->match(
    static fn($server) => $server,
    static fn() => throw new \RuntimeException('Unable to create socket'),
);
$select = Select::timeoutAfter(new ElapsedPeriod(100))
    ->forRead($server);

do {
    $select()
        ->flatMap(fn($ready) => $ready->toRead()->find(fn($stream) => $stream === $server))
        ->flatMap(fn($server) => $server->accept())
        ->match(
            static fn($incomingConnection) => $doSomething($incomingConnection),
            static fn() => null, // no incoming connection within the last 100 milliseconds
        )
} while (true);

The example above creates a socket at /tmp/my-socket.sock and will wait indefinitely. It will call $doSomething() with an incoming connection as soon as one is available., (*4)

Client example:, (*5)

use Innmind\Socket\{
    Client\Unix,
    Address\Unix as Address,
};

$client = Unix::of(Address::of('/tmp/my-socket'))->match(
    static fn($client) => $client,
    static fn() => throw new \RuntimeException('Unable to connect to socket'),
);
$client->write(Str::of('hello there!'))->match(
    static fn($client) => $continueToDoSomething($client),
    static fn($error) => null, // do something else when it failed to write to the socket
);

This will simply connect to the socket server declared above and will send the data hello there!., (*6)

If you want to read what the server send back you should use the stream Select in order to wait for the data to arrive., (*7)

Internet socket

Same logic as the unix socket except the way you build the server:, (*8)

use Innmind\Socket\{
    Server\Internet,
    Internet\Transport,
};
use Innmind\IP\IPv4;
use Innmind\Url\Authority\Port;

$server = Internet::of(
    Transport::tcp(),
    IPv4::of('127.0.0.1'),
    Port::of(80),
);
//this will listen for incoming tcp connection on the port 80

and the client:, (*9)

use Innmind\Socket\{
    Client\Internet,
    Internet\Transport,
};
use Innmind\Url\Url;

$client = Internet::of(
    Transport::tcp(),
    Url::of('//127.0.0.1:80')->authority(),
);
//this will connect to a local socket on port 80

The Versions