Skip to content

Add examples #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ provided to the listen method:
$socket->listen(1337, '192.168.0.1');
```

See also the [examples](examples).

Here's a client that outputs the output of said server and then attempts to
send it a string.
For anything more complex, consider using the
Expand Down
27 changes: 27 additions & 0 deletions examples/01-echo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

// Just start this server and connect to it. Everything you send to it will be
// sent back to you.
//
// $ php examples/01-echo.php 8000
// $ telnet localhost 8000

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;

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

$loop = Factory::create();

$server = new Server($loop);
$server->listen(isset($argv[1]) ? $argv[1] : 0);

$server->on('connection', function (ConnectionInterface $conn) use ($loop) {
echo '[connected]' . PHP_EOL;
$conn->pipe($conn);
});

echo 'Listening on ' . $server->getPort() . PHP_EOL;

$loop->run();
49 changes: 49 additions & 0 deletions examples/02-chat-server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

// Just start this server and connect with any number of clients to it.
// Everything a client sends will be broadcasted to all connected clients.
//
// $ php examples/02-chat-server.php 8000
// $ telnet localhost 8000

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;

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

$loop = Factory::create();

$server = new Server($loop);
$server->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');

$clients = array();

$server->on('connection', function (ConnectionInterface $client) use (&$clients) {
// keep a list of all connected clients
$clients []= $client;
$client->on('close', function() use ($client, &$clients) {
unset($clients[array_search($client, $clients)]);
});

// whenever a new message comes in
$client->on('data', function ($data) use ($client, &$clients) {
// remove any non-word characters (just for the demo)
$data = trim(preg_replace('/[^\w\d \.\,\-\!\?]/u', '', $data));

// ignore empty messages
if ($data === '') {
return;
}

// prefix with client IP and broadcast to all connected clients
$data = $client->getRemoteAddress() . ': ' . $data . PHP_EOL;
foreach ($clients as $client) {
$client->write($data);
}
});
});

echo 'Listening on ' . $server->getPort() . PHP_EOL;

$loop->run();
44 changes: 44 additions & 0 deletions examples/03-benchmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

// Just start the server and connect to it. It will count the number of bytes
// sent for each connection and will print the average throughput once the
// connection closes.
//
// $ php examples/03-benchmark.php 8000
// $ telnet localhost 8000
// $ echo hello world | nc -v localhost 8000
// $ dd if=/dev/zero bs=1M count=1000 | nc -v localhost 8000

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;

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

$loop = Factory::create();

$server = new Server($loop);
$server->listen(isset($argv[1]) ? $argv[1] : 0);

$server->on('connection', function (ConnectionInterface $conn) use ($loop) {
echo '[connected]' . PHP_EOL;

// count the number of bytes received from this connection
$bytes = 0;
$conn->on('data', function ($chunk) use (&$bytes) {
$bytes += strlen($chunk);
});

// report average throughput once client disconnects
$t = microtime(true);
$conn->on('close', function () use ($conn, $t, &$bytes) {
$t = microtime(true) - $t;
echo '[disconnected after receiving ' . $bytes . ' bytes in ' . round($t, 3) . 's => ' . round($bytes / $t / 1024 / 1024, 1) . ' MiB/s]' . PHP_EOL;
});
});

$server->on('error', 'printf');

echo 'bound to ' . $server->getPort() . PHP_EOL;

$loop->run();
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