feat: 本次提交更新内容如下
php有问题,覆盖下
This commit is contained in:
@@ -30,6 +30,8 @@ class Socket
|
||||
'force_client_ids' => [],
|
||||
// 限制允许读取日志的client_id
|
||||
'allow_client_ids' => [],
|
||||
//输出到浏览器默认展开的日志级别
|
||||
'expand_level' => ['debug'],
|
||||
];
|
||||
|
||||
protected $css = [
|
||||
@@ -41,14 +43,17 @@ class Socket
|
||||
];
|
||||
|
||||
protected $allowForceClientIds = []; //配置强制推送且被授权的client_id
|
||||
protected $app;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param array $config 缓存参数
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $config 缓存参数
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
public function __construct(App $app, array $config = [])
|
||||
{
|
||||
$this->app = $app;
|
||||
|
||||
if (!empty($config)) {
|
||||
$this->config = array_merge($this->config, $config);
|
||||
}
|
||||
@@ -57,7 +62,7 @@ class Socket
|
||||
/**
|
||||
* 调试输出接口
|
||||
* @access public
|
||||
* @param array $log 日志信息
|
||||
* @param array $log 日志信息
|
||||
* @return bool
|
||||
*/
|
||||
public function save(array $log = [], $append = false)
|
||||
@@ -65,12 +70,14 @@ class Socket
|
||||
if (!$this->check()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$trace = [];
|
||||
if (App::$debug) {
|
||||
$runtime = round(microtime(true) - THINK_START_TIME, 10);
|
||||
|
||||
if ($this->app->isDebug()) {
|
||||
$runtime = round(microtime(true) - $this->app->getBeginTime(), 10);
|
||||
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
|
||||
$time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
|
||||
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
|
||||
$memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
|
||||
$memory_str = ' [内存消耗:' . $memory_use . 'kb]';
|
||||
$file_load = ' [文件加载:' . count(get_included_files()) . ']';
|
||||
|
||||
@@ -79,6 +86,7 @@ class Socket
|
||||
} else {
|
||||
$current_uri = 'cmd:' . implode(' ', $_SERVER['argv']);
|
||||
}
|
||||
|
||||
// 基本信息
|
||||
$trace[] = [
|
||||
'type' => 'group',
|
||||
@@ -89,10 +97,11 @@ class Socket
|
||||
|
||||
foreach ($log as $type => $val) {
|
||||
$trace[] = [
|
||||
'type' => 'groupCollapsed',
|
||||
'type' => in_array($type, $this->config['expand_level']) ? 'group' : 'groupCollapsed',
|
||||
'msg' => '[ ' . $type . ' ]',
|
||||
'css' => isset($this->css[$type]) ? $this->css[$type] : '',
|
||||
];
|
||||
|
||||
foreach ($val as $msg) {
|
||||
if (!is_string($msg)) {
|
||||
$msg = var_export($msg, true);
|
||||
@@ -103,6 +112,7 @@ class Socket
|
||||
'css' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$trace[] = [
|
||||
'type' => 'groupEnd',
|
||||
'msg' => '',
|
||||
@@ -116,11 +126,13 @@ class Socket
|
||||
'msg' => '[ file ]',
|
||||
'css' => '',
|
||||
];
|
||||
|
||||
$trace[] = [
|
||||
'type' => 'log',
|
||||
'msg' => implode("\n", get_included_files()),
|
||||
'css' => '',
|
||||
];
|
||||
|
||||
$trace[] = [
|
||||
'type' => 'groupEnd',
|
||||
'msg' => '',
|
||||
@@ -135,6 +147,7 @@ class Socket
|
||||
];
|
||||
|
||||
$tabid = $this->getClientArg('tabid');
|
||||
|
||||
if (!$client_id = $this->getClientArg('client_id')) {
|
||||
$client_id = '';
|
||||
}
|
||||
@@ -148,16 +161,18 @@ class Socket
|
||||
} else {
|
||||
$this->sendToClient($tabid, $client_id, $trace, '');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送给指定客户端
|
||||
* @access protected
|
||||
* @author Zjmainstay
|
||||
* @param $tabid
|
||||
* @param $client_id
|
||||
* @param $logs
|
||||
* @param $force_client_id
|
||||
* @param $tabid
|
||||
* @param $client_id
|
||||
* @param $logs
|
||||
* @param $force_client_id
|
||||
*/
|
||||
protected function sendToClient($tabid, $client_id, $logs, $force_client_id)
|
||||
{
|
||||
@@ -167,20 +182,25 @@ class Socket
|
||||
'logs' => $logs,
|
||||
'force_client_id' => $force_client_id,
|
||||
];
|
||||
|
||||
$msg = @json_encode($logs);
|
||||
$address = '/' . $client_id; //将client_id作为地址, server端通过地址判断将日志发布给谁
|
||||
|
||||
$this->send($this->config['host'], $msg, $address);
|
||||
}
|
||||
|
||||
protected function check()
|
||||
{
|
||||
$tabid = $this->getClientArg('tabid');
|
||||
|
||||
//是否记录日志的检查
|
||||
if (!$tabid && !$this->config['force_client_ids']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//用户认证
|
||||
$allow_client_ids = $this->config['allow_client_ids'];
|
||||
|
||||
if (!empty($allow_client_ids)) {
|
||||
//通过数组交集得出授权强制推送的client_id
|
||||
$this->allowForceClientIds = array_intersect($allow_client_ids, $this->config['force_client_ids']);
|
||||
@@ -195,6 +215,7 @@ class Socket
|
||||
} else {
|
||||
$this->allowForceClientIds = $this->config['force_client_ids'];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -211,6 +232,7 @@ class Socket
|
||||
if (!isset($_SERVER[$key])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($args)) {
|
||||
if (!preg_match('/SocketLog\((.*?)\)/', $_SERVER[$key], $match)) {
|
||||
$args = ['tabid' => null];
|
||||
@@ -218,32 +240,39 @@ class Socket
|
||||
}
|
||||
parse_str($match[1], $args);
|
||||
}
|
||||
|
||||
if (isset($args[$name])) {
|
||||
return $args[$name];
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $host - $host of socket server
|
||||
* @param string $message - 发送的消息
|
||||
* @param string $address - 地址
|
||||
* @access protected
|
||||
* @param string $host - $host of socket server
|
||||
* @param string $message - 发送的消息
|
||||
* @param string $address - 地址
|
||||
* @return bool
|
||||
*/
|
||||
protected function send($host, $message = '', $address = '/')
|
||||
{
|
||||
$url = 'http://' . $host . ':' . $this->port . $address;
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
|
||||
$headers = [
|
||||
"Content-Type: application/json;charset=UTF-8",
|
||||
];
|
||||
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置header
|
||||
|
||||
return curl_exec($ch);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user