Skip to content

Commit 1be49ff

Browse files
committed
Add echo example with public echo server
1 parent 97f7a0a commit 1be49ff

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

examples/aerys.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
// Connects to the websocket endpoint in demo.php provided with Aerys (https://github.com/amphp/aerys).
99
Amp\Loop::run(function () {
10-
/** @var \Amp\Websocket\Connection $connection */
10+
/** @var Websocket\Connection $connection */
1111
$connection = yield Websocket\connect('ws://localhost:1337/ws');
1212
yield $connection->send('Hello!');
1313

1414
$i = 0;
1515

16+
/** @var Websocket\Message $message */
1617
while ($message = yield $connection->receive()) {
1718
$payload = yield $message->buffer();
1819

examples/kaazing-echo-demo.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
require dirname(__DIR__) . '/vendor/autoload.php';
4+
5+
use Amp\Delayed;
6+
use Amp\Websocket;
7+
8+
Amp\Loop::run(function () {
9+
/** @var Websocket\Connection $connection */
10+
$connection = yield Websocket\connect('ws://demos.kaazing.com/echo');
11+
yield $connection->send('Hello!');
12+
13+
$i = 0;
14+
15+
/** @var Websocket\Message $message */
16+
while ($message = yield $connection->receive()) {
17+
$payload = yield $message->buffer();
18+
19+
printf("Received: %s\n", $payload);
20+
21+
if ($payload === 'Goodbye!') {
22+
$connection->close();
23+
break;
24+
}
25+
26+
yield new Delayed(1000);
27+
28+
if ($i < 3) {
29+
yield $connection->send('Ping: ' . ++$i);
30+
} else {
31+
yield $connection->send('Goodbye!');
32+
}
33+
}
34+
});

0 commit comments

Comments
 (0)