-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathserver-broadcast.php
More file actions
executable file
·52 lines (40 loc) · 1.05 KB
/
server-broadcast.php
File metadata and controls
executable file
·52 lines (40 loc) · 1.05 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
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Check dependencies
*/
if( ! extension_loaded('sockets' ) ) {
echo "This example requires sockets extension (http://www.php.net/manual/en/sockets.installation.php)\n";
exit(-1);
}
if( ! extension_loaded('pcntl' ) ) {
echo "This example requires PCNTL extension (http://www.php.net/manual/en/pcntl.installation.php)\n";
exit(-1);
}
/**
* Connection handler
*/
function onConnect( $client ) {
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
return $pid;
}
printf( "[%s] Connected at port %d\n", $client->getAddress(), $client->getPort() );
$client->connected();
$read = '';
while( true ) {
$read = $client->read();
if( $read == '' ) {
break;
}
$client->sendBroadcast( $read );
}
$client->disconnected();
printf( "[%s] Disconnected\n", $client->getAddress() );
}
require "sock/SocketServerBroadcast.php";
$server = new \Sock\SocketServerBroadcast();
$server->init();
$server->setConnectionHandler( 'onConnect' );
$server->listen();