swoole结合thinkphp5实现毫秒级定时器

我有一把折断的剑,一条荆棘的路,一个看不清的未来和一场回不去的梦。
### 创建命令行类

创建application/console/Timer.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
65
66
67
68
69
70
71
72
<?php

namespace app\Console;


use think\console\Command;
use think\console\Input;
use think\console\Output;

class Timer extends Command
{
protected $server;
// 命令行配置函数
protected function configure()
{
// setName 设置命令行名称
// setDescription 设置命令行描述
$this->setName('timer:start')->setDescription('Start TCP(Timer) Server!');
}
// 设置命令返回信息
protected function execute(Input $input, Output $output)
{
$this->server = new \swoole_server('0.0.0.0', 9501);
$this->server->set([
'worker_num' => 4,
'daemonize' => false,
]);
$this->server->on('Start', [$this, 'onStart']);
$this->server->on('WorkerStart', [$this, 'onWorkerStart']);
$this->server->on('Connect', [$this, 'onConnect']);
$this->server->on('Receive', [$this, 'onReceive']);
$this->server->on('Close', [$this, 'onClose']);
$this->server->start();
// $output->writeln("TCP: Start.\n");
}
// 主进程启动时回调函数
public function onStart(\swoole_server $server)
{
echo "Start" . PHP_EOL;
}
// Worker 进程启动时回调函数
public function onWorkerStart(\swoole_server $server, $worker_id)
{
// 仅在第一个 Worker 进程启动时启动 Timer 定时器
if ($worker_id == 0) {
// 启动 Timer,每 1000 毫秒回调一次 onTick 函数,
swoole_timer_tick(1000, [$this, 'onTick']);
}
}
// 定时任务函数
public function onTick($timer_id, $params = null)
{
echo 'Hello' . PHP_EOL;
}
// 建立连接时回调函数
public function onConnect(\swoole_server $server, $fd, $from_id)
{
echo "Connect" . PHP_EOL;
}
// 收到信息时回调函数
public function onReceive(\swoole_server $server, $fd, $from_id, $data)
{
echo "message: {$data} form Client: {$fd}" . PHP_EOL;
// 将受到的客户端消息再返回给客户端
$server->send($fd, "Message form Server: ".$data);
}
// 关闭连时回调函数
public function onClose(\swoole_server $server, $fd, $from_id)
{
echo "Close" . PHP_EOL;
}
}

注册命令

文件所在 application/command.php

1
2
3
4
5
<?php

return [
'app\console\Timer',
];

接下来就可以通过命令行来启动毫秒定时器

1
php think timer:start

查看结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@iZ2ze20vl8jnphmrpet8txZ tp5]# php think timer:start
Start
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

http://www.iswoole.com/article/2067