1 Star 1 Fork 1

amsds/Joyboo-admin-easyswoole

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
EasySwooleEvent.php 7.56 KB
一键复制 编辑 原始数据 按行查看 历史
豪气冲云天 提交于 2022-03-25 14:43 . 链路追踪 && 复发请求
<?php
namespace EasySwoole\EasySwoole;
use App\Common\Classes\CtxRequest;
use App\Common\HttpTracker\Index as HttpTracker;
use App\Websocket\Events;
use App\Websocket\Parser;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Crontab\Crontab;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\RedisPool\RedisPool;
use EasySwoole\Socket\Dispatcher;
use EasySwoole\Socket\Config as SocketConfig;
use Swoole\Websocket\Server as WSserver;
use Swoole\WebSocket\Frame;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\ORM\Db\Config as ORMConfig;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;
use EasySwoole\I18N\I18N;
use EasySwoole\Component\Di;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// date_default_timezone_set('Asia/Shanghai');
// 注册异常处理器
\EasySwoole\EasySwoole\Trigger::getInstance(new \App\Common\Handler\Trigger());
Config::getInstance()->loadDir(EASYSWOOLE_ROOT . '/App/Common/Config');
// mysql连接池
$mysqlCfg = config('MYSQL');
foreach ($mysqlCfg as $mname => $mvalue)
{
$MysqlConfig = new ORMConfig($mvalue);
DbManager::getInstance()->addConnection(new Connection($MysqlConfig), $mname);
}
self::dbOnQuery();
//redis连接池注册
$redisCfg = config('REDIS');
foreach ($redisCfg as $rname => $rvalue)
{
$RedisConfig = new RedisConfig($rvalue);
RedisPool::getInstance()->register($RedisConfig, $rname);
}
// 注册全局onRequest回调
self::httpGlobalOnRequest();
// 注册SwooleTable(WebSocket连接符管理)
\App\Common\Classes\FdManager::getInstance()->register();
}
public static function mainServerCreate(EventRegister $register)
{
self::setWebsocet($register);
// 热重载
self::hotReload();
// WeChatManager::getInstance()->register();
}
/**
* Websocket解析器、事件
* @param EventRegister $register
* @throws \EasySwoole\Socket\Exception\Exception
*/
protected static function setWebsocet(EventRegister $register)
{
// 创建一个 Dispatcher 配置
$conf = new SocketConfig();
// 设置 Dispatcher 为 WebSocket 模式
$conf->setType(SocketConfig::WEB_SOCKET);
// 设置解析器对象
$conf->setParser(new Parser());
// 创建 Dispatcher 对象 并注入 config 对象
$dispatch = new Dispatcher($conf);
// 给server 注册相关事件 在 WebSocket 模式下 on message 事件必须注册 并且交给 Dispatcher 对象处理
$register->set(EventRegister::onMessage, function (WSserver $server, Frame $frame) use ($dispatch) {
$dispatch->dispatch($server, $frame->data, $frame);
});
// 注册服务事件
$register->add(EventRegister::onOpen, [Events::class, 'onOpen']);
$register->add(EventRegister::onClose, [Events::class, 'onClose']);
$register->add(EventRegister::onWorkerError, [Events::class, 'onError']);
// 注册自定义进程
include_once EASYSWOOLE_ROOT . '/App/CustomProcess/init.php';
// 自动注册Crontab任务(粒度为分钟级,秒级或毫秒级请使用Timer)
Crontab::getInstance()->addTask(\App\Crontab\AutoRegister::class);
}
/**
* 热重载
* reload的是worker进程,master和manager进程不会reload
*/
public static function hotReload()
{
// 只允许在开发环境热重载
if (Core::getInstance()->runMode() === 'dev')
{
$watcher = new \EasySwoole\FileWatcher\FileWatcher();
// // 设置监控规则和监控目录
$rule = new \EasySwoole\FileWatcher\WatchRule(EASYSWOOLE_ROOT . "/App");
$watcher->addRule($rule);
$watcher->setOnChange(function () {
var_dump('检测到/App目录文件变更, worker进程reload ...');
$Server = ServerManager::getInstance()->getSwooleServer();
// worker进程reload不会触发客户端的断线重连,但是原来的fd已经不可用了,手动close
foreach ($Server->connections as $fd)
{
// 不要在 close 之后写清理逻辑。应当放置到 onClose 回调中处理
$Server->close($fd);
}
$Server->reload();
});
$watcher->attachServer(ServerManager::getInstance()->getSwooleServer());
}
}
public static function httpGlobalOnRequest()
{
I18N::getInstance()->addLanguage(new \App\Common\Languages\Chinese(), 'zh');
I18N::getInstance()->addLanguage(new \App\Common\Languages\English(), 'en');
I18N::getInstance()->setDefaultLanguage('zh');
Di::getInstance()->set(
SysConst::HTTP_GLOBAL_ON_REQUEST,
function (Request $request, Response $response) {
CtxRequest::getInstance()->setRequest($request);
// 获取 header 中 language 参数
if ($request->hasHeader('accept-language')) {
$langage = $request->getHeader('accept-language');
if (is_array($langage)) {
$langage = current($langage);
}
foreach (['zh', 'en'] as $lang) {
if (stripos($langage, $lang) !== false) {
I18N::getInstance()->setDefaultLanguage($lang);
break;
}
}
}
// 开启链路追踪
$point = HttpTracker::getInstance()->createStart('Joyboo:onRequest');
$point && $point->setStartArg(HttpTracker::startArgsRequest($request));
return true;
});
Di::getInstance()->set(SysConst::HTTP_GLOBAL_AFTER_REQUEST,
function (Request $request, Response $response) {
$point = HttpTracker::getInstance()->startPoint();
$point && $point->setEndArg(HttpTracker::endArgsResponse($response))->end();
});
}
/**
* Mysql连接池全局onQuery事件
*/
public static function dbOnQuery()
{
DbManager::getInstance()->onQuery(function (
\EasySwoole\ORM\Db\Result $result,
\EasySwoole\Mysqli\QueryBuilder $builder,
$start) {
$sql = $builder->getLastQuery();
if (empty($sql))
{
return;
}
trace($sql, 'info', 'sql');
// 不记录的SQL,表名
$logtable = config('NOT_WRITE_SQL.table');
foreach($logtable as $v)
{
if (
strpos($sql, "`$v`")
||
// 支持 XXX*这种模糊匹配
(strpos($v, '*') && strpos($sql, '`' . str_replace('*', '', $v)))
)
{
return;
}
}
// 不记录的SQL,正则
$not = config('NOT_WRITE_SQL.pattern');
foreach ($not as $pattern)
{
if (preg_match($pattern, $sql))
{
return;
}
}
/** @var \App\Model\LogSql $Log */
$Log = model('LogSql');
$Log->sqlWriteLog($sql);
});
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/weboo_admin/Joyboo-admin-easyswoole.git
[email protected]:weboo_admin/Joyboo-admin-easyswoole.git
weboo_admin
Joyboo-admin-easyswoole
Joyboo-admin-easyswoole
main

搜索帮助