Skip to content

Commit 090367c

Browse files
committed
Update to latest websocket changes
1 parent 24daf3c commit 090367c

9 files changed

+58
-48
lines changed

composer.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,22 @@
3333
"amphp/amp": "^3",
3434
"amphp/byte-stream": "^2",
3535
"amphp/http": "^2",
36-
"amphp/http-client": "^5",
36+
"amphp/http-client": "^5-dev",
3737
"amphp/socket": "^2",
38-
"amphp/websocket": "^2",
38+
"amphp/websocket": "^2-dev",
3939
"league/uri": "^6",
4040
"psr/http-message": "^1",
4141
"revolt/event-loop": "^1"
4242
},
4343
"require-dev": {
4444
"amphp/http-server": "^3",
45-
"amphp/websocket-server": "^3",
45+
"amphp/websocket-server": "^3-dev",
4646
"amphp/phpunit-util": "^3",
4747
"amphp/php-cs-fixer-config": "^2",
4848
"phpunit/phpunit": "^9",
4949
"psr/log": "^1",
5050
"psalm/phar": "^5.4"
5151
},
52-
"minimum-stability": "dev",
53-
"prefer-stable": true,
5452
"autoload": {
5553
"psr-4": {
5654
"Amp\\Websocket\\Client\\": "src"

docs/index.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use Amp\Websocket\Client;
7171
Amp\Loop::run(function () {
7272
/** @var Client\WebsocketConnection $connection */
7373
$connection = yield Client\connect('ws://demos.kaazing.com/echo');
74-
yield $connection->send('Hello!');
74+
yield $connection->sendText('Hello!');
7575

7676
$i = 0;
7777

@@ -89,9 +89,9 @@ Amp\Loop::run(function () {
8989
yield new Delayed(1000);
9090

9191
if ($i < 3) {
92-
yield $connection->send('Ping: ' . ++$i);
92+
yield $connection->sendText('Ping: ' . ++$i);
9393
} else {
94-
yield $connection->send('Goodbye!');
94+
yield $connection->sendText('Goodbye!');
9595
}
9696
}
9797
});

examples/amp.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
$connection = connect($handshake);
1515

16-
$connection->send('Hello!');
16+
$connection->sendText('Hello!');
1717

1818
$i = 0;
1919

@@ -30,8 +30,8 @@
3030
delay(1);
3131

3232
if ($i < 3) {
33-
$connection->send('Ping: ' . ++$i);
33+
$connection->sendText('Ping: ' . ++$i);
3434
} else {
35-
$connection->send('Goodbye!');
35+
$connection->sendText('Goodbye!');
3636
}
3737
}

src/Rfc6455Connection.php

+27-15
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use Amp\Http\Client\Response;
1010
use Amp\Socket\SocketAddress;
1111
use Amp\Socket\TlsInfo;
12-
use Amp\Websocket\CloseCode;
1312
use Amp\Websocket\Rfc6455Client;
14-
use Amp\Websocket\WebsocketClientMetadata;
13+
use Amp\Websocket\WebsocketCloseCode;
14+
use Amp\Websocket\WebsocketCount;
1515
use Amp\Websocket\WebsocketMessage;
16+
use Amp\Websocket\WebsocketTimestamp;
17+
use Traversable;
1618

17-
final class Rfc6455Connection implements WebsocketConnection
19+
final class Rfc6455Connection implements WebsocketConnection, \IteratorAggregate
1820
{
1921
use ForbidCloning;
2022
use ForbidSerialization;
@@ -63,11 +65,6 @@ public function isClosedByPeer(): bool
6365
return $this->client->isClosedByPeer();
6466
}
6567

66-
public function getUnansweredPingCount(): int
67-
{
68-
return $this->client->getUnansweredPingCount();
69-
}
70-
7168
public function getCloseCode(): int
7269
{
7370
return $this->client->getCloseCode();
@@ -78,19 +75,19 @@ public function getCloseReason(): string
7875
return $this->client->getCloseReason();
7976
}
8077

81-
public function send(string $data): void
78+
public function sendText(string $data): void
8279
{
83-
$this->client->send($data);
80+
$this->client->sendText($data);
8481
}
8582

8683
public function sendBinary(string $data): void
8784
{
8885
$this->client->sendBinary($data);
8986
}
9087

91-
public function stream(ReadableStream $stream): void
88+
public function streamText(ReadableStream $stream): void
9289
{
93-
$this->client->stream($stream);
90+
$this->client->streamText($stream);
9491
}
9592

9693
public function streamBinary(ReadableStream $stream): void
@@ -103,17 +100,22 @@ public function ping(): void
103100
$this->client->ping();
104101
}
105102

106-
public function getInfo(): WebsocketClientMetadata
103+
public function getCount(WebsocketCount $type): int
104+
{
105+
return $this->client->getCount($type);
106+
}
107+
108+
public function getTimestamp(WebsocketTimestamp $type): int
107109
{
108-
return $this->client->getInfo();
110+
return $this->client->getTimestamp($type);
109111
}
110112

111113
public function isClosed(): bool
112114
{
113115
return $this->client->isClosed();
114116
}
115117

116-
public function close(int $code = CloseCode::NORMAL_CLOSE, string $reason = ''): void
118+
public function close(int $code = WebsocketCloseCode::NORMAL_CLOSE, string $reason = ''): void
117119
{
118120
$this->client->close($code, $reason);
119121
}
@@ -122,4 +124,14 @@ public function onClose(\Closure $onClose): void
122124
{
123125
$this->client->onClose($onClose);
124126
}
127+
128+
public function isCompressionEnabled(): bool
129+
{
130+
return $this->client->isCompressionEnabled();
131+
}
132+
133+
public function getIterator(): Traversable
134+
{
135+
yield from $this->client;
136+
}
125137
}

src/Rfc6455ConnectionFactory.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
use Amp\ForbidSerialization;
77
use Amp\Http\Client\Response;
88
use Amp\Socket\Socket;
9-
use Amp\Websocket\Compression\CompressionContext;
10-
use Amp\Websocket\HeartbeatQueue;
9+
use Amp\Websocket\Compression\WebsocketCompressionContext;
1110
use Amp\Websocket\Parser\Rfc6455ParserFactory;
1211
use Amp\Websocket\Parser\WebsocketParserFactory;
13-
use Amp\Websocket\RateLimiter;
1412
use Amp\Websocket\Rfc6455Client;
13+
use Amp\Websocket\WebsocketHeartbeatQueue;
14+
use Amp\Websocket\WebsocketRateLimit;
1515

1616
final class Rfc6455ConnectionFactory implements WebsocketConnectionFactory
1717
{
1818
use ForbidCloning;
1919
use ForbidSerialization;
2020

2121
public function __construct(
22-
private readonly ?HeartbeatQueue $heartbeatQueue = null,
23-
private readonly ?RateLimiter $rateLimiter = null,
22+
private readonly ?WebsocketHeartbeatQueue $heartbeatQueue = null,
23+
private readonly ?WebsocketRateLimit $rateLimit = null,
2424
private readonly WebsocketParserFactory $parserFactory = new Rfc6455ParserFactory(
2525
messageSizeLimit: Rfc6455Connection::DEFAULT_MESSAGE_SIZE_LIMIT,
2626
frameSizeLimit: Rfc6455Connection::DEFAULT_FRAME_SIZE_LIMIT,
@@ -33,15 +33,15 @@ public function __construct(
3333
public function createConnection(
3434
Response $handshakeResponse,
3535
Socket $socket,
36-
?CompressionContext $compressionContext = null,
36+
?WebsocketCompressionContext $compressionContext = null,
3737
): WebsocketConnection {
3838
$client = new Rfc6455Client(
3939
socket: $socket,
4040
masked: true,
4141
parserFactory: $this->parserFactory,
4242
compressionContext: $compressionContext,
4343
heartbeatQueue: $this->heartbeatQueue,
44-
rateLimiter: $this->rateLimiter,
44+
rateLimit: $this->rateLimit,
4545
frameSplitThreshold: $this->frameSplitThreshold,
4646
closePeriod: $this->closePeriod,
4747
);

src/Rfc6455Connector.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use Amp\Socket\ConnectContext;
1717
use Amp\Socket\Socket;
1818
use Amp\Websocket;
19-
use Amp\Websocket\Compression\CompressionContextFactory;
2019
use Amp\Websocket\Compression\Rfc7692CompressionFactory;
20+
use Amp\Websocket\Compression\WebsocketCompressionContextFactory;
2121

2222
final class Rfc6455Connector implements WebsocketConnector
2323
{
@@ -27,12 +27,12 @@ final class Rfc6455Connector implements WebsocketConnector
2727
private readonly HttpClient $httpClient;
2828

2929
/**
30-
* @param CompressionContextFactory|null $compressionContextFactory Use null to disable compression.
30+
* @param WebsocketCompressionContextFactory|null $compressionContextFactory Use null to disable compression.
3131
*/
3232
public function __construct(
3333
private readonly WebsocketConnectionFactory $connectionFactory = new Rfc6455ConnectionFactory(),
3434
HttpClient $httpClient = null,
35-
private readonly ?CompressionContextFactory $compressionContextFactory = new Rfc7692CompressionFactory(),
35+
private readonly ?WebsocketCompressionContextFactory $compressionContextFactory = new Rfc7692CompressionFactory(),
3636
) {
3737
$this->httpClient = $httpClient
3838
?? (new HttpClientBuilder)->usingPool(

src/WebsocketConnectionFactory.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
use Amp\Http\Client\Response;
66
use Amp\Socket\Socket;
7-
use Amp\Websocket\Compression\CompressionContext;
7+
use Amp\Websocket\Compression\WebsocketCompressionContext;
88

99
interface WebsocketConnectionFactory
1010
{
1111
/**
1212
* @param Response $handshakeResponse Response that initiated the websocket connection.
1313
* @param Socket $socket Underlying socket to be used for network communication.
14-
* @param CompressionContext|null $compressionContext CompressionContext generated from the response headers.
14+
* @param WebsocketCompressionContext|null $compressionContext CompressionContext generated from the response headers.
1515
*/
1616
public function createConnection(
1717
Response $handshakeResponse,
1818
Socket $socket,
19-
?CompressionContext $compressionContext = null,
19+
?WebsocketCompressionContext $compressionContext = null,
2020
): WebsocketConnection;
2121
}

test-autobahn/runner.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use Amp\Websocket\Client\Rfc6455ConnectionFactory;
55
use Amp\Websocket\Client\Rfc6455Connector;
66
use Amp\Websocket\Client\WebsocketHandshake;
7-
use Amp\Websocket\ClosedException;
87
use Amp\Websocket\Parser\Rfc6455ParserFactory;
8+
use Amp\Websocket\WebsocketClosedException;
99

1010
require __DIR__ . '/../vendor/autoload.php';
1111

@@ -42,10 +42,10 @@
4242
if ($message->isBinary()) {
4343
$connection->sendBinary($content);
4444
} else {
45-
$connection->send($content);
45+
$connection->sendText($content);
4646
}
4747
}
48-
} catch (ClosedException $e) {
48+
} catch (WebsocketClosedException $e) {
4949
// ignore
5050
} catch (AssertionError $e) {
5151
print 'Assertion error: ' . $e->getMessage() . PHP_EOL;

test/WebsocketConnectionTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
use Amp\Socket\SocketException;
1414
use Amp\TimeoutCancellation;
1515
use Amp\Websocket\Client;
16-
use Amp\Websocket\ClosedException;
1716
use Amp\Websocket\Parser\Rfc6455ParserFactory;
1817
use Amp\Websocket\Server\EmptyWebsocketHandshakeHandler;
1918
use Amp\Websocket\Server\Websocket;
2019
use Amp\Websocket\Server\WebsocketClientHandler;
2120
use Amp\Websocket\WebsocketClient;
21+
use Amp\Websocket\WebsocketClosedException;
2222
use Psr\Log\NullLogger;
2323
use function Amp\async;
2424
use function Amp\delay;
@@ -82,14 +82,14 @@ public function testSimpleTextEcho(): void
8282
public function handleClient(WebsocketClient $client, Request $request, Response $response): void
8383
{
8484
while ($message = $client->receive()) {
85-
$client->send($message->buffer());
85+
$client->sendText($message->buffer());
8686
}
8787
}
8888
});
8989

9090
try {
9191
$client = connect('ws://' . $address->toString());
92-
$client->send('Hey!');
92+
$client->sendText('Hey!');
9393

9494
$message = $client->receive();
9595

@@ -110,8 +110,8 @@ public function testUnconsumedMessage(): void
110110
[$server, $address] = $this->createServer(new class implements WebsocketClientHandler {
111111
public function handleClient(WebsocketClient $client, Request $request, Response $response): void
112112
{
113-
$client->send(\str_repeat('.', 1024 * 1024));
114-
$client->send('Message');
113+
$client->sendText(\str_repeat('.', 1024 * 1024));
114+
$client->sendText('Message');
115115
$client->close();
116116
}
117117
});
@@ -188,7 +188,7 @@ public function handleClient(WebsocketClient $client, Request $request, Response
188188
$message->buffer();
189189

190190
self::fail('Buffering the message should have thrown a ClosedException due to exceeding the message size limit');
191-
} catch (ClosedException $exception) {
191+
} catch (WebsocketClosedException $exception) {
192192
$this->assertSame('Received payload exceeds maximum allowable size', $exception->getReason());
193193
} finally {
194194
$server->stop();

0 commit comments

Comments
 (0)