-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32-socket-chat.php
More file actions
42 lines (32 loc) · 1.14 KB
/
Copy path32-socket-chat.php
File metadata and controls
42 lines (32 loc) · 1.14 KB
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
42
<?php
use React\Socket\ConnectionInterface;
use React\Socket\LimitingServer;
use React\Socket\SocketServer;
require __DIR__ . '/vendor/autoload.php';
$socket = new SocketServer('127.0.0.1:8010');
$socket = new LimitingServer($socket, null);
$socket->on('connection', function (ConnectionInterface $connection) use ($socket) {
foreach ($socket->getConnections() as $c) {
if ($c === $connection) {
continue;
}
$c->write($connection->getLocalAddress() . ' s\'est connecté' . PHP_EOL);
}
$connection->on('data', function ($data) use ($connection, $socket) {
if ($data === '') {
return;
}
$data = $connection->getLocalAddress() . ' à dit : ' . $data . PHP_EOL;
foreach ($socket->getConnections() as $c) {
$c->write($data);
}
});
$connection->on('close', function () use ($connection, $socket) {
foreach ($socket->getConnections() as $c) {
if ($c === $connection) {
continue;
}
$c->write($connection->getLocalAddress() . ' s\'est déconnecté' . PHP_EOL);
}
});
});