Skip to content

AbstractTask

Base class for WebSocket tasks.

Tasks get typed access to the WebSocket console, router, and dispatcher through PhalconKit injectable properties. Concrete tasks should implement action methods such as listenAction().


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

public initialize(): void

handleWebSocket

public handleWebSocket(): void

listenAction

public listenAction(): void

initializeOpen

public initializeOpen(): void

initializeMessage

public initializeMessage(): void

initializeClose

public initializeClose(): void

initializeWorkerError

public initializeWorkerError(): void

initializeStart

public initializeStart(): void

initializeWorkerStart

public initializeWorkerStart(): void

initializeShutdown

public initializeShutdown(): void

initializeRequest

public initializeRequest(): void

initializePipeMessage

public initializePipeMessage(): void

onOpen

public onOpen(\Swoole\WebSocket\Server $server, \Swoole\Http\Request $request): void

Parameters:

Parameter Type Description
$server \Swoole\WebSocket\Server
$request \Swoole\Http\Request

onMessage

public onMessage(\Swoole\WebSocket\Server $server, \Swoole\WebSocket\Frame $frame): void

Parameters:

Parameter Type Description
$server \Swoole\WebSocket\Server
$frame \Swoole\WebSocket\Frame

onClose

public onClose(\Swoole\WebSocket\Server $server, int $fd): void

Parameters:

Parameter Type Description
$server \Swoole\WebSocket\Server
$fd int

onWorkerError

public onWorkerError(\Swoole\WebSocket\Server $server, int $fd, int $code, string $reason): void

Parameters:

Parameter Type Description
$server \Swoole\WebSocket\Server
$fd int
$code int
$reason string

onStart

public onStart(\Swoole\WebSocket\Server $server): void

Parameters:

Parameter Type Description
$server \Swoole\WebSocket\Server

onWorkerStart

public onWorkerStart(\Swoole\WebSocket\Server $server, int $workerId): void

Parameters:

Parameter Type Description
$server \Swoole\WebSocket\Server
$workerId int

onShutdown

public onShutdown(\Swoole\WebSocket\Server $server): void

Parameters:

Parameter Type Description
$server \Swoole\WebSocket\Server

onRequest

public onRequest(\Swoole\Http\Request $request, \Swoole\Http\Response $response): void

Parameters:

Parameter Type Description
$request \Swoole\Http\Request
$response \Swoole\Http\Response

onPipeMessage

public onPipeMessage(\Swoole\WebSocket\Server $server, int $srcWorkerId, mixed $data): void

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.