-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
59 lines (53 loc) · 1.57 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
55
56
57
58
59
<?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,));
$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Workerstart', array($this, 'onWorkerstart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));
$this->serv->start();
}
public function onStart($serv)
{
echo "Start\n";
}
public function onWorkerstart(swoole_server $serv, $fd)
{
$this->redis = new Redis();
$this->redis->connect('127.0.0.1', 6379);
}
public function onConnect($serv, $fd, $from_id)
{
$serv->send($fd, "set name:{$fd}!");
}
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
$name = trim($this->redis->get($fd));
if (!$name) {
$this->redis->set($fd, $data);
} else {
foreach ($serv->connections as $value) {
if ($fd != $value) {
$serv->send($value, $name . ':' . "$data");
}
}
}
$data = trim($data);
if($data=='quit'){
$serv->stop();
$serv->shutdown();
}
}
public function onClose($serv, $fd, $from_id)
{
$this->redis->del($fd);
echo "Client {$fd} close connection\n";
}
}
$server = new Server();