-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
54 lines (52 loc) · 1.33 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
$serv = new swoole_server("127.0.0.1", 9502);
$serv->set(array('worker_num' => 4));
$serv->on('timer', function($serv, $interval) {
echo "onTimer: $interval\n";
});
$serv->on('workerStart', function($serv, $worker_id) {
//if($worker_id == 0) $serv->addtimer(600);
});
$serv->on('connect', function ($serv, $fd){
//echo "[#".posix_getpid()."]\tClient:Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$start_fd = 0;
$stime = time();
/*$data = json_decode($data,true);
switch($data['cmd']){
case "login":
$serv->send($fd, "welcome !");
break;
}*/
while(true)
{
$conn_list = $serv->connection_list($start_fd, 30);
if($conn_list===false)
{
//echo "finish\n";
break;
}
//var_dump($conn_list);
$start_fd = end($conn_list);
//var_dump($conn_list);
foreach($conn_list as $fdv)
{
if($fdv == $fd){
continue;
}
var_dump($fdv);
$serv->send($fdv, "broadcast");
}
}
//echo "[#".posix_getpid()."]\tClient[$fd]: $data\n";
//$serv->send($fd, "swoole: $data");
$etime = time();
echo $etime - $stime;
//$serv->close($fd);
});
$serv->on('close', function ($serv, $fd) {
//echo "[#".posix_getpid()."]\tClient: Close.\n";
});
$serv->start();
?>