forked from reactphp/socket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-echo.php
More file actions
41 lines (31 loc) · 1021 Bytes
/
01-echo.php
File metadata and controls
41 lines (31 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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
//
// You can also run a secure TLS echo server like this:
//
// $ php examples/01-echo.php 8000 examples/localhost.pem
// $ openssl s_client -connect localhost:8000
use React\EventLoop\Factory;
use React\Socket\TcpServer;
use React\Socket\ConnectionInterface;
use React\Socket\SecureServer;
require __DIR__ . '/../vendor/autoload.php';
$loop = Factory::create();
$server = new TcpServer(isset($argv[1]) ? $argv[1] : 0, $loop);
// secure TLS mode if certificate is given as second parameter
if (isset($argv[2])) {
$server = new SecureServer($server, $loop, array(
'local_cert' => $argv[2]
));
}
$server->on('connection', function (ConnectionInterface $conn) {
echo '[connected]' . PHP_EOL;
$conn->pipe($conn);
});
$server->on('error', 'printf');
echo 'Listening on ' . $server->getAddress() . PHP_EOL;
$loop->run();