Skip to content

Commit 3fca60f

Browse files
authored
Merge pull request #49 from clue-labs/examples
Add examples
2 parents cd09f16 + 359d70f commit 3fca60f

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ provided to the listen method:
4646
$socket->listen(1337, '192.168.0.1');
4747
```
4848

49+
See also the [examples](examples).
50+
4951
Here's a client that outputs the output of said server and then attempts to
5052
send it a string.
5153
For anything more complex, consider using the

examples/01-echo.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
// Just start this server and connect to it. Everything you send to it will be
4+
// sent back to you.
5+
//
6+
// $ php examples/01-echo.php 8000
7+
// $ telnet localhost 8000
8+
9+
use React\EventLoop\Factory;
10+
use React\Socket\Server;
11+
use React\Socket\ConnectionInterface;
12+
13+
require __DIR__ . '/../vendor/autoload.php';
14+
15+
$loop = Factory::create();
16+
17+
$server = new Server($loop);
18+
$server->listen(isset($argv[1]) ? $argv[1] : 0);
19+
20+
$server->on('connection', function (ConnectionInterface $conn) use ($loop) {
21+
echo '[connected]' . PHP_EOL;
22+
$conn->pipe($conn);
23+
});
24+
25+
echo 'Listening on ' . $server->getPort() . PHP_EOL;
26+
27+
$loop->run();

examples/02-chat-server.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
// Just start this server and connect with any number of clients to it.
4+
// Everything a client sends will be broadcasted to all connected clients.
5+
//
6+
// $ php examples/02-chat-server.php 8000
7+
// $ telnet localhost 8000
8+
9+
use React\EventLoop\Factory;
10+
use React\Socket\Server;
11+
use React\Socket\ConnectionInterface;
12+
13+
require __DIR__ . '/../vendor/autoload.php';
14+
15+
$loop = Factory::create();
16+
17+
$server = new Server($loop);
18+
$server->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');
19+
20+
$clients = array();
21+
22+
$server->on('connection', function (ConnectionInterface $client) use (&$clients) {
23+
// keep a list of all connected clients
24+
$clients []= $client;
25+
$client->on('close', function() use ($client, &$clients) {
26+
unset($clients[array_search($client, $clients)]);
27+
});
28+
29+
// whenever a new message comes in
30+
$client->on('data', function ($data) use ($client, &$clients) {
31+
// remove any non-word characters (just for the demo)
32+
$data = trim(preg_replace('/[^\w\d \.\,\-\!\?]/u', '', $data));
33+
34+
// ignore empty messages
35+
if ($data === '') {
36+
return;
37+
}
38+
39+
// prefix with client IP and broadcast to all connected clients
40+
$data = $client->getRemoteAddress() . ': ' . $data . PHP_EOL;
41+
foreach ($clients as $client) {
42+
$client->write($data);
43+
}
44+
});
45+
});
46+
47+
echo 'Listening on ' . $server->getPort() . PHP_EOL;
48+
49+
$loop->run();

examples/03-benchmark.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
// Just start the server and connect to it. It will count the number of bytes
4+
// sent for each connection and will print the average throughput once the
5+
// connection closes.
6+
//
7+
// $ php examples/03-benchmark.php 8000
8+
// $ telnet localhost 8000
9+
// $ echo hello world | nc -v localhost 8000
10+
// $ dd if=/dev/zero bs=1M count=1000 | nc -v localhost 8000
11+
12+
use React\EventLoop\Factory;
13+
use React\Socket\Server;
14+
use React\Socket\ConnectionInterface;
15+
16+
require __DIR__ . '/../vendor/autoload.php';
17+
18+
$loop = Factory::create();
19+
20+
$server = new Server($loop);
21+
$server->listen(isset($argv[1]) ? $argv[1] : 0);
22+
23+
$server->on('connection', function (ConnectionInterface $conn) use ($loop) {
24+
echo '[connected]' . PHP_EOL;
25+
26+
// count the number of bytes received from this connection
27+
$bytes = 0;
28+
$conn->on('data', function ($chunk) use (&$bytes) {
29+
$bytes += strlen($chunk);
30+
});
31+
32+
// report average throughput once client disconnects
33+
$t = microtime(true);
34+
$conn->on('close', function () use ($conn, $t, &$bytes) {
35+
$t = microtime(true) - $t;
36+
echo '[disconnected after receiving ' . $bytes . ' bytes in ' . round($t, 3) . 's => ' . round($bytes / $t / 1024 / 1024, 1) . ' MiB/s]' . PHP_EOL;
37+
});
38+
});
39+
40+
$server->on('error', 'printf');
41+
42+
echo 'bound to ' . $server->getPort() . PHP_EOL;
43+
44+
$loop->run();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy