跳到主要内容

WebSocket客户端

概述

ripple提供了一个易用的WebSocketClient组件,可以用于创建WebSocket客户端连接。

use Ripple\WebSocket\Client;
use function Co\wait;

$connection = new Client('wss://echo.websocket.org');
$connection->onOpen(function (Connection $connection) {
$connection->send('{"action":"ping","data":[]}');
});

$connection->onMessage(function (string $data, Connection $connection) {
echo 'Received: ' . $data . \PHP_EOL;
});

$connection->onClose(function (Connection $connection) {
echo 'Connection closed' . \PHP_EOL;
});

wait();