首先看一下运行环境
swoole version 4.8.11
php version 7.4.33
easyswoole version 3.4.6
在easyswoole 的入口文件 如下:
php
<?php
namespace EasySwoole\EasySwoole;
use App\WebSocket\WebSocketEvents;
use App\WebSocket\WebSocketParser;
use EasySwoole\Component\Process\Manager;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Config;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use \EasySwoole\Component\Di;
class EasySwooleEvent implements Event
{
public static function initialize()
{
date_default_timezone_set('Asia/Shanghai');
// 加载Config下所有配置
self::loadConfig();
}
public static function mainServerCreate(EventRegister $register)
{
// 热重载监视器
$hotReloadOptions = new \EasySwoole\HotReload\HotReloadOptions;
$hotReload = new \EasySwoole\HotReload\HotReload($hotReloadOptions);
$hotReloadOptions->setMonitorFolder([EASYSWOOLE_ROOT . '/App']);
$server = ServerManager::getInstance()->getSwooleServer();
$hotReload->attachToServer($server);
// websocket注册
self::InitWebSocket($register);
// 注册 FastCache缓存
$register->add($register::onWorkerStart, function (\swoole_server $server, int $workerId) {
});
}
public static function InitWebSocket(EventRegister $register)
{
$config = new \EasySwoole\Socket\Config();
$config->setType($config::WEB_SOCKET);
$config->setParser(WebSocketParser::class);
$dispatcher = new \EasySwoole\Socket\Dispatcher($config);
$config->setOnExceptionHandler(function (
\Swoole\Server $server,
\Throwable $throwable,
string $raw,
\EasySwoole\Socket\Client\WebSocket $client,
\EasySwoole\Socket\Bean\Response $response
) {
$response->setMessage('system error!' . $throwable->getMessage());
$response->setStatus($response::STATUS_RESPONSE_AND_CLOSE);
});
$register->set($register::onMessage,
function (\Swoole\Websocket\Server $server, \Swoole\Websocket\Frame $frame) use ($dispatcher) {
$dispatcher->dispatch($server, $frame->data, $frame);
});
// 注册服务事件
$register->add(EventRegister::onOpen, [WebSocketEvents::class, 'onOpen']);
$register->add(EventRegister::onClose, [WebSocketEvents::class, 'onClose']);
}
/**
* Description:加载自定义配置文件
*/
public static function loadConfig()
{
Config::getInstance()->loadDir(EASYSWOOLE_ROOT . '/Config');
}
}
启动服务 命令:
bash
php easyswoole server start
此处我监听的是App 目录
当我修改了App目录下任意一个文件 保存后
FileScanner hot reload initialize at Linux in PID 878774
但easyswoole 监听某个目录 不生效了
真的是 我查了半天,包括做减法,只保留easyswoole 框架最小代码,结果还是不行,突然想起前几天编译了php 的一个grpc 的扩展 于是我猜测会不会是php 环境所导致。
于是乎 修改php.ini 去掉grpc.so 的扩展
然后在重启php-fpm 后
重启easyswoole 服务后
修改文件后出现 HOT_RELOAD: reloaded at 1704433881
热重载生效了 到现在还没搞明白啥情况啊 晕了