phasync
WebSocket / receive

Method

WebSocket::receive

The next text or binary message, or null once the connection is closed.

Description#

public WebSocket::receive(): ?string

Closed by the client, by end(), or because the client broke the protocol. When the worker stops, swerve closes it (1001, or 1012 on a reload) and this throws a WorkerStoppingException. Waits for a message. It is the alternative to $onMessage, and the two don't mix.

Return values#

?string

Errors / Exceptions#

ExceptionWhen
LogicExceptionWhile $onMessage has listeners: they get the messages

Examples#

Example 1
while (null !== ($message = $ws->receive())) {
    $ws->send(strrev($message));
}

See also#