-
Notifications
You must be signed in to change notification settings - Fork 729
/
Copy pathswoole_after_server.php
64 lines (50 loc) · 1.6 KB
/
swoole_after_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
55
56
57
58
59
60
61
62
63
64
<?php
class Server
{
private $serv;
public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 8,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 2,
'debug_mode'=> 1,
));
$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));
// bind callback
$this->serv->on('Timer' , array($this, 'onTimer'));
$this->serv->start();
}
public function onStart( $serv ) {
echo "Start\n";
}
public function onConnect( $serv, $fd, $from_id ) {
echo "Client {$fd} connect\n";
}
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
echo "Get Message From Client {$fd}:{$data}\n";
$param = array(
'fd' => $fd,
'msg' => $data
);
$str = json_encode( $param );
$serv->after( 1000 , array($this, 'onAfter') , $str );
}
public function onClose( $serv, $fd, $from_id ) {
echo "Client {$fd} close connection\n";
}
public function onAfter( $data ) {
$param = json_decode( $data, true );
$this->serv->send( $param['fd'] , $param['msg']);
}
public function onTimer( $serv, $interval ) {
// Do nothing.
}
}
new Server();
// use src/o2/swoole_async_client to test the server.
?>