HelPHP

Server
in package
uses Logs

The WebSocket server: one stream, one loop, the connections it accepts.

Built on the stream layer (stream_socket_server, stream_select) rather than ext/sockets, for one reason that matters: TLS becomes a configuration (ssl:// plus a certificate in $settings) instead of a rewrite, and a proxy can terminate it without the code knowing.

The loop is strictly non-blocking and symmetrical: every connection has a read buffer and a write queue, stream_select watches both, and nothing writes directly to a socket. A client that stops reading therefore delays only itself, which is the whole point of writing the queue by hand.

The server knows no helPHP: no Config, no DB, no user. It calls back.

$server = new Server(['host'=>'127.0.0.1', 'port'=>8090, 'origins'=>['https://site.test']]);
$server->on('open',    function (Connection $c) { … });
$server->on('message', function (Connection $c, $payload, $opcode) { $c->send($payload); });
$server->on('close',   function (Connection $c, $code, $reason) { … });
$server->run();

The bootstrap of the instance, the OID checks and the routing to the REST API are the caller's business: they live in the instance's run_websocket.php and in the bridge, not here.

Tags
see

Handshake, Connection, Frame

Table of Contents

Methods

__construct()  : mixed
add_log_sink()  : void
Adds a destination. A sink that throws is reported and skipped: a logger must never be the reason a connection dies.
address()  : string
client()  : Connection|null
clients()  : array<int, Connection>
has_log_sinks()  : bool
on()  : void
Registers a callback.
run()  : void
Opens the listening stream and runs the loop until stop().
stop()  : void
Asks the loop to stop after the current iteration.

Methods

__construct()

public __construct([array<string|int, mixed> $settings = array() ]) : mixed
Parameters
$settings : array<string|int, mixed> = array()

See self::$defaults; unknown keys are ignored.

add_log_sink()

Adds a destination. A sink that throws is reported and skipped: a logger must never be the reason a connection dies.

public add_log_sink(mixed $callback) : void
Parameters
$callback : mixed

address()

public address() : string
Return values
string —

Listening address, e.g. "tcp://127.0.0.1:8090".

has_log_sinks()

public has_log_sinks() : bool
Return values
bool —

True when the lines go to an application sink rather than the instance log.

on()

Registers a callback.

public on(string $event, callable $callback) : void

Events: open (Connection), message (Connection, string $payload, int $opcode), close (Connection, int $code, string $reason), tick (Server, fired after every loop iteration: the hook a runner uses for its own timers, e.g. a dev-mode file scan), log (string $line). A callback that throws is logged and its connection is closed 1011: a bug in the application must not take the server down.

Parameters
$event : string

Event name.

$callback : callable
Tags
throws
InvalidArgumentException

On an unknown event.

run()

Opens the listening stream and runs the loop until stop().

public run() : void
Tags
throws
RuntimeException

When the address cannot be bound.

stop()

Asks the loop to stop after the current iteration.

public stop() : void
On this page

Search results