Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
API Reference
A single-page index of every public type. For the deeper "why", see the linked concept pages.
finalclass Socket
{
publicstaticfunctionserver(
Transport$transport,
string$host,
int$port,
?Domain$domain = null,
?float$timeout = null,
): SocketServerInterface;
publicstaticfunctionclient(
Transport$transport,
string$host,
int$port,
?Domain$domain = null,
?float$timeout = null,
): SocketClientInterface;
}Domain is honoured only by TCP / UDP. $timeout is honoured only by TLS / SSL.
interface SocketServerInterface
{
publicfunctiongetHost(): string;
publicfunctiongetPort(): int;
publicfunctiongetSocket(): mixed; // \Socket | resource | null/** @return array<int|string, SocketConnectionInterface> */publicfunctiongetClients(): array;
publicfunctionlisten(): static;
publicfunctionclose(): bool;
publicfunctionlive(callable$callback, float$idleSeconds = 0.05): void;
publicfunctiontick(callable$callback, float$waitSeconds = 0.0): int;
publicfunctionstop(): void;
publicfunctionisRunning(): bool;
/** @param int|string|array<int, int|string>|null $clients */publicfunctionbroadcast(string$message, int|string|array|null$clients = null): bool;
publicfunctionregister(int|string$id, SocketConnectionInterface$client): bool;
publicfunctionwait(float$seconds): void;
}Adds:
abstractclass AbstractServer implements SocketServerInterface
{
protectedfunctionaddClient(SocketConnectionInterface$client): int; // returns the internal keyprotectedfunctionevict(int$key): void;
protectedfunctionindexOf(SocketConnectionInterface$client): ?int;
protectedstaticfunctionsplitSeconds(float$seconds): array; // [whole, microseconds]
}Concrete ext-sockets-backed servers. TCP additionally exposes:
publicfunction backlog(int$backlog): self; // default 8, must be >= 1The parent of TLS and SSL. Adds:
publicfunction option(string$key, mixed$value): static;
publicfunction timeout(float$seconds): static;
publicfunction blocking(bool$mode = true): static;
publicfunction crypto(?CryptoMethod$method): static;TLS and SSL themselves carry no extra public surface — see Transport TLS / Transport SSL.
interface SocketClientInterface
{
publicfunctiongetHost(): string;
publicfunctiongetPort(): int;
publicfunctiongetSocket(): mixed;
publicfunctionconnect(): static;
publicfunctiondisconnect(): bool;
publicfunctionread(int$length = 1024): ?string;
publicfunctionwrite(string$data): ?int;
}publicfunction read(int$length = 1024, int$type = PHP_BINARY_READ): ?string;($type accepts PHP_BINARY_READ or PHP_NORMAL_READ.)
publicfunction read(int$length = 1024, int$flags = 0): ?string;
publicfunction write(string$data, int$flags = 0): ?int;($flags accepts the standard MSG_* bitmasks.)
The parent of Client\TLS and Client\SSL. Adds:
publicfunction option(string$key, mixed$value): static;
publicfunction timeout(float$seconds): static;
publicfunction blocking(bool$mode = true): static;
publicfunction crypto(?CryptoMethod$method): static;interface SocketConnectionInterface
{
publicfunctionsetId(int|string$id): static;
publicfunctiongetId(): int|string|null;
publicfunctionread(int$length = 1024): ?string;
publicfunctionwrite(string$data): ?int;
publicfunctionclose(): bool;
publicfunctionisAlive(): bool;
publicfunctiongetSocket(): mixed;
publicfunctiongetChannel(): ChannelInterface;
}The only implementation in the package is InitPHP\Socket\Server\ServerConnection.
interface ChannelInterface
{
publicfunctionread(int$length = 1024, ?int$flag = null): ?string;
publicfunctionwrite(string$data): ?int;
publicfunctionclose(): bool;
publicfunctionisAlive(): bool;
publicfunctiongetResource(): mixed;
}Built-in implementations:
| Class | Backing | Notes |
|---|---|---|
Channel\TcpChannel | ext-sockets | Liveness via `MSG_PEEK |
Channel\UdpChannel | ext-sockets (shared listening socket) | Per-peer buffer drained by read(). Exposes feed(), getPeerHost(), getPeerPort(), peerKey(). |
Channel\StreamChannel | stream resource | Liveness via feof(). |
See Enums for the full reference.
InitPHP\Socket\Enum\Transport// TCP, UDP, TLS, SSL
InitPHP\Socket\Enum\Domain // V4, V6, UNIXInitPHP\Socket\Enum\CryptoMethod// SSLv2/3/23, ANY, TLS, TLSv1_0/1_1/1_2See Exceptions for the full reference.
InitPHP\Socket\Exception\SocketExceptionInterface// marker
InitPHP\Socket\Exception\SocketException// \RuntimeException
InitPHP\Socket\Exception\SocketConnectionException// \RuntimeException
InitPHP\Socket\Exception\SocketListenException// \RuntimeException
InitPHP\Socket\Exception\SocketInvalidArgumentException// \InvalidArgumentException- Architecture — what each type does and why.
- Server Lifecycle / Client Lifecycle — when to call each method.
initphp/socket · MIT · PHP 8.1+ · part of the InitPHP family · file issues at InitPHP/Socket/issues
Getting started
Transports
Concepts
Reference
Recipes
Operational