Nexus API Reference

SwooleConnectionContext
in package
implements WebSocketContext

FinalYes
Tags
psalm-api

Worker-mode WebSocketContext — pushes directly to the local Swoole WebSocket server. One instance per connection.

Table of Contents

Interfaces

WebSocketContext
Runtime context for an active WebSocket connection.

Methods

__construct()  : mixed
close()  : void
Close the WebSocket connection with an optional status code and reason.
id()  : int
Returns the numeric connection identifier assigned by the server.
isAlive()  : bool
Returns true if the connection is still open and accepting frames.
request()  : ServerRequestInterface
Returns the original HTTP upgrade request that initiated the connection.
send()  : void
Send a UTF-8 text frame to the client.
sendBinary()  : void
Send a binary frame to the client.
sendPing()  : void
Send a WebSocket ping frame to check connection liveness.
withRequest()  : WebSocketContext
Return a copy of the context with a different underlying PSR-7 request.

Methods

__construct()

public __construct(Server $server, int $fd, ServerRequestInterface $request) : mixed
Parameters
$server : Server
$fd : int
$request : ServerRequestInterface

close()

Close the WebSocket connection with an optional status code and reason.

public close([int $code = 1000 ][, string $reason = '' ]) : void
Parameters
$code : int = 1000

WebSocket close code (default 1000 = normal closure).

$reason : string = ''

Human-readable reason phrase sent to the client.

id()

Returns the numeric connection identifier assigned by the server.

public id() : int
Return values
int

isAlive()

Returns true if the connection is still open and accepting frames.

public isAlive() : bool
Tags
psalm-suppress

MixedReturnStatement,MixedInferredReturnType

Return values
bool

request()

Returns the original HTTP upgrade request that initiated the connection.

public request() : ServerRequestInterface

Useful for reading headers, cookies, or query parameters set during the WebSocket handshake (e.g. authentication tokens or channel parameters).

Return values
ServerRequestInterface

send()

Send a UTF-8 text frame to the client.

public send(string $text) : void
Parameters
$text : string

The UTF-8 encoded payload to transmit.

sendBinary()

Send a binary frame to the client.

public sendBinary(string $data) : void
Parameters
$data : string

The raw binary payload to transmit.

sendPing()

Send a WebSocket ping frame to check connection liveness.

public sendPing() : void

The client should respond with a pong frame automatically.

withRequest()

Return a copy of the context with a different underlying PSR-7 request.

public withRequest(ServerRequestInterface $request) : WebSocketContext

The dispatcher uses this to attach FastRoute path-param attributes to the stored request BEFORE the actor sees the context — otherwise a channel actor could not look up $conn->request()->getAttribute('id') for a route like /ws/games/{id}.

Parameters
$request : ServerRequestInterface
Return values
WebSocketContext

        
On this page

Search results