Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 720 Bytes

Server Websockets.md

File metadata and controls

28 lines (23 loc) · 720 Bytes

Websockets

You can upgrade an http connection to websockets RFC 6455 using websocket().

// src/api/get.php
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use Amp\Websocket\Server\WebsocketClientHandler;
use Amp\Websocket\WebsocketClient;
use function CatPaw\Web\websocket;

$handler = new class implements WebsocketClientHandler {
    public function handleClient(
        WebsocketClient $client,
        Request $request,
        Response $response,
    ):void {
        $client->sendText("welcome!");

        foreach ($client as $message) {
            echo $message.PHP_EOL;
        }
    }
};

return static fn () => websocket($handler);