AbstractTask
WebSocket task with overridable Swoole event hooks.
Requires the shared swoole DI service to provide a WebSocket server using positional event arguments (event_object disabled). Request/message callbacks reset model connection state before invoking application hooks. Subscriptions belong to the current worker process; applications coordinate other workers.
- Full name:
\PhalconKit\Modules\Ws\Tasks\AbstractTask - Parent class:
\PhalconKit\Modules\Ws\Task - This class is an Abstract class
Properties
subscriptions
public static array $subscriptions
- This property is static.
onOpen
public \Closure $onOpen
onClose
public \Closure $onClose
onMessage
public \Closure $onMessage
onWorkerError
public \Closure $onWorkerError
onStart
public \Closure $onStart
onWorkerStart
public \Closure $onWorkerStart
onShutdown
public \Closure $onShutdown
onRequest
public \Closure $onRequest
onPipeMessage
public \Closure $onPipeMessage
server
public \Swoole\WebSocket\Server $server
Methods
initialize
Resolve the shared swoole service, build callbacks, and register them.
public initialize(): void
Call the parent when overriding initialization to retain event dispatch.
handleWebSocket
Register the initialized callbacks on the server without starting its event loop.
public handleWebSocket(): void
listenAction
Start the configured server event loop; returns after the server stops.
public listenAction(): void
initializeOpen
Install the open callback, resetting model connection state before the hook.
public initializeOpen(): void
initializeMessage
Install the message callback, resetting model connection state before the hook.
public initializeMessage(): void
initializeClose
Install the close callback, resetting model connection state before the hook.
public initializeClose(): void
initializeWorkerError
Adapt Swoole's five worker-error arguments to the existing four-argument hook.
public initializeWorkerError(): void
Keep onWorkerError() overrides compatible while passing the actual exit code and retaining both the worker PID and termination signal in the reason text.
initializeStart
Install the master-process startup callback.
public initializeStart(): void
initializeWorkerStart
Install the worker startup callback with its worker ID.
public initializeWorkerStart(): void
initializeShutdown
Install the server shutdown callback.
public initializeShutdown(): void
initializeRequest
Install the HTTP callback, resetting model connection state before the hook.
public initializeRequest(): void
initializePipeMessage
Install the inter-worker message callback, resetting model connection state first.
public initializePipeMessage(): void
onOpen
Handle a completed WebSocket handshake; the default logs the client descriptor.
public onOpen(\Swoole\WebSocket\Server $server, \Swoole\Http\Request $request): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$server | \Swoole\WebSocket\Server | |
$request | \Swoole\Http\Request |
onMessage
Handle a received WebSocket frame; the default logs its descriptor and payload.
public onMessage(\Swoole\WebSocket\Server $server, \Swoole\WebSocket\Frame $frame): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$server | \Swoole\WebSocket\Server | |
$frame | \Swoole\WebSocket\Frame |
onClose
Handle a closed client descriptor; override to clean application subscription state.
public onClose(\Swoole\WebSocket\Server $server, int $fd): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$server | \Swoole\WebSocket\Server | |
$fd | int |
onWorkerError
Handle a failed worker; override to integrate application monitoring.
public onWorkerError(\Swoole\WebSocket\Server $server, int $fd, int $code, string $reason): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$server | \Swoole\WebSocket\Server | Server whose worker failed. |
$fd | int | Worker ID, despite the historical parameter name; not a client descriptor. |
$code | int | Worker exit code. |
$reason | string | Worker process details, formatted as "pid= |
onStart
Handle master-process startup; the default logs the listening address.
public onStart(\Swoole\WebSocket\Server $server): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$server | \Swoole\WebSocket\Server |
onWorkerStart
Handle worker startup; override for resources owned by this worker process.
public onWorkerStart(\Swoole\WebSocket\Server $server, int $workerId): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$server | \Swoole\WebSocket\Server | |
$workerId | int |
onShutdown
Handle server shutdown; the default logs completion.
public onShutdown(\Swoole\WebSocket\Server $server): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$server | \Swoole\WebSocket\Server |
onRequest
Handle an HTTP request on the WebSocket server.
public onRequest(\Swoole\Http\Request $request, \Swoole\Http\Response $response): void
The default logs the path and ends the response with a placeholder body.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$request | \Swoole\Http\Request | |
$response | \Swoole\Http\Response |
onPipeMessage
Handle data sent by another worker.
public onPipeMessage(\Swoole\WebSocket\Server $server, int $srcWorkerId, mixed $data): void
The default logs string-compatible data; override for structured messages.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$server | \Swoole\WebSocket\Server | |
$srcWorkerId | int | |
$data | mixed |
subscribeClientToChannel
Subscribes a client, identified by its file descriptor, to a specific channel.
public subscribeClientToChannel(int $fd, string $channel): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$fd | int | The file descriptor identifying the client. |
$channel | string | The name of the channel to subscribe the client to. |
unsubscribeClientFromChannel
Unsubscribes a client, identified by its file descriptor, from a specific channel.
public unsubscribeClientFromChannel(int $fd, string $channel): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$fd | int | The file descriptor identifying the client. |
$channel | string | The name of the channel to unsubscribe the client from. |
broadcastToChannel
Broadcasts a message to all active subscribers of a specified channel. Optionally, the broadcast can target a specific list of file descriptors.
public broadcastToChannel(\Swoole\WebSocket\Server $server, string $channel, array $data, array|null $fdList = null): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$server | \Swoole\WebSocket\Server | The server instance used to handle broadcasting and validating connections. |
$channel | string | The channel name to which the message should be broadcasted. |
$data | array | The message payload to be sent to the subscribers. |
$fdList | array|null | Optional list of file descriptors to restrict the broadcast to specific clients. |
unsubscribeClient
Unsubscribes a client, identified by its file descriptor, from all subscribed channels.
public unsubscribeClient(int $fd): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$fd | int | The file descriptor identifying the client. |
log
Logs a message with the worker ID of the specified server instance or the default server instance.
public log(string $message, \Swoole\WebSocket\Server|null $server = null): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The message to log. |
$server | \Swoole\WebSocket\Server|null | The server instance to use for retrieving the worker ID. If null, the default server instance will be used. |
Inherited methods
resetConnectionState
Clear request-scoped model connection state in a long-running worker.
public resetConnectionState(): void
Call this before custom WebSocket callbacks that perform model reads or writes. The built-in abstract task invokes it for open, message, close, HTTP request, and pipe-message callbacks.