-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathEasySwooleEvent.php
101 lines (85 loc) · 3.66 KB
/
EasySwooleEvent.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace EasySwoole\EasySwoole;
use App\Process\HotReload;
use App\Utility\Pool\MysqlPool;
use App\Utility\Pool\RedisPool;
use App\WebSocket\WebSocketEvents;
use EasySwoole\Component\Pool\PoolManager;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\FastCache\Cache;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\Socket\Dispatcher;
use App\WebSocket\WebSocketParser;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
/**
* Mysql协程连接池
*/
$mysqlConf = PoolManager::getInstance()->register(MysqlPool::class, Config::getInstance()->getConf('MYSQL.POOL_MAX_NUM'));
if ($mysqlConf === null) {
//当返回null时,代表注册失败,无法进行再次的配置修改
//注册失败不一定要抛出异常,因为内部实现了自动注册,不需要注册也能使用
throw new \Exception('注册失败!');
}
//设置其他参数
$mysqlConf->setMaxObjectNum(20)->setMinObjectNum(5);
/**
* REDIS协程连接池
*/
PoolManager::getInstance()->register(RedisPool::class, Config::getInstance()->getConf('REDIS.POOL_MAX_NUM'));
}
public static function mainServerCreate(EventRegister $register)
{
/**
* **************** websocket控制器 **********************
*/
// 创建一个 Dispatcher 配置
$conf = new \EasySwoole\Socket\Config();
// 设置 Dispatcher 为 WebSocket 模式
$conf->setType(\EasySwoole\Socket\Config::WEB_SOCKET);
// 设置解析器对象
$conf->setParser(new WebSocketParser());
// 创建 Dispatcher 对象 并注入 config 对象
$dispatch = new Dispatcher($conf);
// 给server 注册相关事件 在 WebSocket 模式下 on message 事件必须注册 并且交给 Dispatcher 对象处理
$register->set(EventRegister::onMessage, function (\swoole_websocket_server $server, \swoole_websocket_frame $frame) use ($dispatch) {
$dispatch->dispatch($server, $frame->data, $frame);
});
// 注册服务事件
$register->add(EventRegister::onOpen, [WebSocketEvents::class, 'onOpen']);
$register->add(EventRegister::onClose, [WebSocketEvents::class, 'onClose']);
/**
* **************** mysql 热启动 ****************
*/
$register->add($register::onWorkerStart, function (\swoole_server $server, int $workerId) {
if ($server->taskworker == false) {
//每个worker进程都预创建连接
PoolManager::getInstance()->getPool(MysqlPool::class)->preLoad(5);//最小创建数量
}
});
/**
* **************** 服务热重启 ****************
*/
$swooleServer = ServerManager::getInstance()->getSwooleServer();
$swooleServer->addProcess((new HotReload('HotReload', ['disableInotify' => false]))->getProcess());
/**
* **************** 缓存服务 ****************
*/
Cache::getInstance()->setTempDir(EASYSWOOLE_TEMP_DIR)->attachToServer(ServerManager::getInstance()->getSwooleServer());
}
public static function onRequest(Request $request, Response $response): bool
{
// TODO: Implement onRequest() method.
return true;
}
public static function afterRequest(Request $request, Response $response): void
{
// TODO: Implement afterAction() method.
}
}