-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.php
34 lines (25 loc) · 887 Bytes
/
server.php
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
<?php
// nc localhost 4000
use React\ExampleChatWithRandomStranger\AppInterface;
use React\ExampleChatWithRandomStranger\LoggingApp;
use React\ExampleChatWithRandomStranger\PairApp;
use React\ExampleChatWithRandomStranger\TextApp;
require 'vendor/autoload.php';
$logger = new Monolog\Logger('ExampleChatWithRandomStranger');
$logger->pushHandler(new Monolog\Handler\StreamHandler(STDOUT));
$app = new LoggingApp(
new TextApp(new PairApp()),
$logger
);
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$i = 0;
$names = ['Alice', 'Bob', 'Carol', 'Dave', 'Erin', 'Frank', 'Eve',
'Mallory', 'Oscar', 'Peggy', 'Trent', 'Walter'];
$socket->on('connection', function ($conn) use (&$i, $names, $app) {
$conn->id = isset($names[$i]) ? $names[$i] : $i;
$app->connect($conn);
$i++;
});
$socket->listen(4000);
$loop->run();