Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ca8cc081bc | |||
| a31c24ddf3 | |||
| 826503f4e6 | |||
| eeb348c053 | |||
| 168954e4c4 | |||
| 6a27fa3b74 | |||
| 79d4f47a67 | |||
| e6786666b7 | |||
| b5c18fc074 | |||
| 86b9c46755 | |||
| baea90e9e5 | |||
| 87e7535f27 | |||
| cc073f13c2 | |||
| 3afb1bd514 | |||
| 0e7d429bfc | |||
| 526aa9a63d | |||
| 48db719890 | |||
| daa8311eb9 | |||
| a065e5da49 | |||
| 8ae2af7dd6 | |||
| 6f0376ac44 | |||
| 08a4d6af4e | |||
| 9899578d52 | |||
| f97df5cfe5 | |||
| 34f8f6f523 | |||
| f6f94aff32 | |||
| 377a9c17c3 | |||
| 174d2f1659 | |||
| d6c8380c64 | |||
| 2e54326aba | |||
| 59632ac4a3 | |||
| da5a6a6b83 | |||
| d758d21f08 | |||
| c59212da78 | |||
| 9ce0cb96bf | |||
| c7b5d5fb59 | |||
| daa89f3794 | |||
| 60670f7f97 | |||
| 3a0c5f6d70 | |||
| ef312c2aa4 | |||
| a0eb46cf4e | |||
| fbd3799081 | |||
| 97a3335217 | |||
| 1790d01730 | |||
| 8a4b62b2d8 |
@@ -89,7 +89,7 @@ class Kiri
|
|||||||
if (Kiri::getPlatform()->isMac()) {
|
if (Kiri::getPlatform()->isMac()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$name = '[' . \config('id', 'system-service') . ']';
|
$name = '[' . \config('site.id', 'system-service') . ']';
|
||||||
if (!empty($prefix)) {
|
if (!empty($prefix)) {
|
||||||
$name .= '.' . $prefix;
|
$name .= '.' . $prefix;
|
||||||
}
|
}
|
||||||
@@ -104,7 +104,7 @@ class Kiri
|
|||||||
public static function getStoragePath(): string
|
public static function getStoragePath(): string
|
||||||
{
|
{
|
||||||
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR;
|
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR;
|
||||||
$path = \config('storage', $default);
|
$path = \config('site.log.path', $default);
|
||||||
if (!is_dir($path)) {
|
if (!is_dir($path)) {
|
||||||
mkdir($path, 0777, true);
|
mkdir($path, 0777, true);
|
||||||
}
|
}
|
||||||
|
|||||||
+135
@@ -0,0 +1,135 @@
|
|||||||
|
# Kiri Core Framework Documentation
|
||||||
|
|
||||||
|
## Project Introduction
|
||||||
|
|
||||||
|
Kiri is a high-performance PHP-based core framework designed to provide developers with a simple and efficient development experience. The framework includes a variety of practical utility classes, exception handling mechanisms, configuration management, and a dependency injection container, making it suitable for building various types of applications.
|
||||||
|
|
||||||
|
### Key Features
|
||||||
|
- **Dependency Injection Container**: Provides powerful dependency management via `Kiri\Di\Container`.
|
||||||
|
- **Configuration Management**: Supports reading configuration from files and environment variables.
|
||||||
|
- **Exception Handling**: Offers a unified exception handling interface with support for registering custom exception handlers.
|
||||||
|
- **Logging**: Built-in `StdoutLogger` supporting multiple log levels.
|
||||||
|
- **Utility Classes**:
|
||||||
|
- `Str`: String operations such as encryption and random string generation.
|
||||||
|
- `Json`: JSON encoding/decoding and response construction.
|
||||||
|
- `Help`: Auxiliary functions including XML conversion, random number generation, and email sending.
|
||||||
|
- `DateFormat`: Date and time formatting tools.
|
||||||
|
- **Networking**: Supports local IP retrieval and Socket programming.
|
||||||
|
- **NoSQL Support**: Provides client wrappers for Redis and MongoDB with connection pool management.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### System Requirements
|
||||||
|
- PHP 7.4 or higher
|
||||||
|
- Composer (for dependency management)
|
||||||
|
|
||||||
|
### Installation Steps
|
||||||
|
1. Ensure Composer is installed.
|
||||||
|
2. Run the following command to install dependencies:
|
||||||
|
```bash
|
||||||
|
composer install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage Guide
|
||||||
|
|
||||||
|
### Initialize the Framework
|
||||||
|
```php
|
||||||
|
use Kiri\Kiri;
|
||||||
|
use Kiri\Application;
|
||||||
|
|
||||||
|
// Initialize the container
|
||||||
|
Kiri::setContainer(new Container());
|
||||||
|
|
||||||
|
// Create an application instance
|
||||||
|
$app = new Application(
|
||||||
|
new EventProvider(),
|
||||||
|
new ConfigProvider(),
|
||||||
|
Kiri::getContainer()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Initialize the application
|
||||||
|
$app->init();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuration Management
|
||||||
|
Configuration files can be loaded and accessed via the `ConfigProvider` class:
|
||||||
|
```php
|
||||||
|
$config = new ConfigProvider();
|
||||||
|
$databaseHost = $config->get('database.host');
|
||||||
|
```
|
||||||
|
|
||||||
|
### Dependency Injection
|
||||||
|
Use the container to inject dependencies:
|
||||||
|
```php
|
||||||
|
$service = Kiri::getContainer()->get(MyService::class);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Logging
|
||||||
|
Use the built-in logger to output messages:
|
||||||
|
```php
|
||||||
|
Kiri::getLogger()->info('This is an info message.');
|
||||||
|
```
|
||||||
|
|
||||||
|
### Exception Handling
|
||||||
|
Register a custom exception handler:
|
||||||
|
```php
|
||||||
|
$errorHandler = new ErrorHandler();
|
||||||
|
$errorHandler->registerExceptionHandler(function (Throwable $e) {
|
||||||
|
echo "Unhandled exception: " . $e->getMessage();
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Database Connections
|
||||||
|
|
||||||
|
#### Redis
|
||||||
|
```php
|
||||||
|
$redis = new Redis();
|
||||||
|
$redis->connect();
|
||||||
|
$redis->set('key', 'value');
|
||||||
|
echo $redis->get('key');
|
||||||
|
```
|
||||||
|
|
||||||
|
#### MongoDB
|
||||||
|
```php
|
||||||
|
$mongo = new MongoDB();
|
||||||
|
$collection = $mongo->getCollection('users');
|
||||||
|
$result = $collection->find([]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Utility Class Examples
|
||||||
|
|
||||||
|
#### String Operations
|
||||||
|
```php
|
||||||
|
echo Str::rand(10); // Generate a random string
|
||||||
|
```
|
||||||
|
|
||||||
|
#### JSON Response
|
||||||
|
```php
|
||||||
|
echo Json::jsonSuccess(['data' => 'example'], 'Operation successful');
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Date Formatting
|
||||||
|
```php
|
||||||
|
echo DateFormat::DaySecond(); // Get the number of seconds in a day
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contribution Guidelines
|
||||||
|
|
||||||
|
Contributions are welcome! Please follow these steps:
|
||||||
|
1. Fork the project repository.
|
||||||
|
2. Create a new branch (`git checkout -b feature/new-feature`)
|
||||||
|
3. Commit your changes (`git commit -am 'Add new feature'`)
|
||||||
|
4. Push to the branch (`git push origin feature/new-feature`)
|
||||||
|
5. Create a Pull Request
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is licensed under the MIT License. For details, see the [LICENSE](LICENSE) file.
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
|
||||||
|
If you have any questions or suggestions, please open an Issue or contact the project maintainers.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Thank you for choosing Kiri Core Framework! We look forward to your feedback and contributions.
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
# Kiri 核心框架文档
|
||||||
|
|
||||||
|
## 项目介绍
|
||||||
|
|
||||||
|
Kiri 是一个基于 PHP 的高性能核心框架,旨在为开发者提供简洁、高效的开发体验。该框架内置了多种实用工具类、异常处理机制、配置管理、容器依赖注入等功能,适用于构建各种类型的应用程序。
|
||||||
|
|
||||||
|
### 主要特性
|
||||||
|
- **依赖注入容器**:通过 `Kiri\Di\Container` 提供强大的依赖管理功能。
|
||||||
|
- **配置管理**:支持从文件和环境变量中读取配置。
|
||||||
|
- **异常处理**:提供统一的异常处理接口,支持注册自定义异常处理器。
|
||||||
|
- **日志记录**:内置 `StdoutLogger` 支持多种日志级别输出。
|
||||||
|
- **实用工具类**:
|
||||||
|
- `Str`:字符串操作,如加密、随机字符串生成等。
|
||||||
|
- `Json`:JSON 编码/解码及响应构造。
|
||||||
|
- `Help`:提供 XML 转换、随机数生成、邮件发送等辅助功能。
|
||||||
|
- `DateFormat`:日期和时间格式化工具。
|
||||||
|
- **网络功能**:支持本地 IP 获取、Socket 编程。
|
||||||
|
- **NoSQL 支持**:提供 Redis 和 MongoDB 的客户端封装,支持连接池管理。
|
||||||
|
|
||||||
|
## 安装
|
||||||
|
|
||||||
|
### 系统要求
|
||||||
|
- PHP 7.4 或更高版本
|
||||||
|
- Composer(用于依赖管理)
|
||||||
|
|
||||||
|
### 安装步骤
|
||||||
|
1. 确保已安装 Composer。
|
||||||
|
2. 执行以下命令安装依赖:
|
||||||
|
```bash
|
||||||
|
composer install
|
||||||
|
```
|
||||||
|
|
||||||
|
## 使用说明
|
||||||
|
|
||||||
|
### 初始化框架
|
||||||
|
```php
|
||||||
|
use Kiri\Kiri;
|
||||||
|
use Kiri\Application;
|
||||||
|
|
||||||
|
// 初始化容器
|
||||||
|
Kiri::setContainer(new Container());
|
||||||
|
|
||||||
|
// 创建应用实例
|
||||||
|
$app = new Application(
|
||||||
|
new EventProvider(),
|
||||||
|
new ConfigProvider(),
|
||||||
|
Kiri::getContainer()
|
||||||
|
);
|
||||||
|
|
||||||
|
// 初始化应用
|
||||||
|
$app->init();
|
||||||
|
```
|
||||||
|
|
||||||
|
### 配置管理
|
||||||
|
配置文件可通过 `ConfigProvider` 类进行加载和访问:
|
||||||
|
```php
|
||||||
|
$config = new ConfigProvider();
|
||||||
|
$databaseHost = $config->get('database.host');
|
||||||
|
```
|
||||||
|
|
||||||
|
### 依赖注入
|
||||||
|
使用容器进行依赖注入:
|
||||||
|
```php
|
||||||
|
$service = Kiri::getContainer()->get(MyService::class);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 日志记录
|
||||||
|
使用内置日志记录器输出信息:
|
||||||
|
```php
|
||||||
|
Kiri::getLogger()->info('This is an info message.');
|
||||||
|
```
|
||||||
|
|
||||||
|
### 异常处理
|
||||||
|
注册自定义异常处理器:
|
||||||
|
```php
|
||||||
|
$errorHandler = new ErrorHandler();
|
||||||
|
$errorHandler->registerExceptionHandler(function (Throwable $e) {
|
||||||
|
echo "Unhandled exception: " . $e->getMessage();
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 数据库连接
|
||||||
|
#### Redis
|
||||||
|
```php
|
||||||
|
$redis = new Redis();
|
||||||
|
$redis->connect();
|
||||||
|
$redis->set('key', 'value');
|
||||||
|
echo $redis->get('key');
|
||||||
|
```
|
||||||
|
|
||||||
|
#### MongoDB
|
||||||
|
```php
|
||||||
|
$mongo = new MongoDB();
|
||||||
|
$collection = $mongo->getCollection('users');
|
||||||
|
$result = $collection->find([]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 工具类使用示例
|
||||||
|
#### 字符串处理
|
||||||
|
```php
|
||||||
|
echo Str::rand(10); // 生成随机字符串
|
||||||
|
```
|
||||||
|
|
||||||
|
#### JSON 响应
|
||||||
|
```php
|
||||||
|
echo Json::jsonSuccess(['data' => 'example'], 'Operation successful');
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 日期格式化
|
||||||
|
```php
|
||||||
|
echo DateFormat::DaySecond(); // 获取一天的秒数
|
||||||
|
```
|
||||||
|
|
||||||
|
## 贡献指南
|
||||||
|
|
||||||
|
欢迎贡献代码!请遵循以下步骤:
|
||||||
|
1. Fork 项目仓库。
|
||||||
|
2. 创建新分支 (`git checkout -b feature/new-feature`)
|
||||||
|
3. 提交更改 (`git commit -am 'Add new feature'`)
|
||||||
|
4. 推送分支 (`git push origin feature/new-feature`)
|
||||||
|
5. 创建 Pull Request
|
||||||
|
|
||||||
|
## 许可证
|
||||||
|
|
||||||
|
该项目采用 MIT 许可证。详细信息请参阅 [LICENSE](LICENSE) 文件。
|
||||||
|
|
||||||
|
## 联系方式
|
||||||
|
|
||||||
|
如有问题或建议,请提交 Issue 或联系项目维护者。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
感谢您选择 Kiri 核心框架!我们期待您的反馈与贡献。
|
||||||
+56
-59
@@ -1,59 +1,56 @@
|
|||||||
{
|
{
|
||||||
"name": "game-worker/kiri-core",
|
"name": "game-worker/kiri-core",
|
||||||
"description": "test framework",
|
"description": "test framework",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "XiangLin",
|
"name": "XiangLin",
|
||||||
"email": "as2252258@163.com"
|
"email": "as2252258@163.com"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.3",
|
"php": ">=8.4",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-fileinfo": "*",
|
"ext-fileinfo": "*",
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
"ext-redis": "*",
|
"ext-redis": "*",
|
||||||
"ext-simplexml": "*",
|
"ext-simplexml": "*",
|
||||||
"ext-libxml": "*",
|
"ext-libxml": "*",
|
||||||
"ext-iconv": "*",
|
"ext-iconv": "*",
|
||||||
"ext-mbstring": "*",
|
"ext-mbstring": "*",
|
||||||
"ext-xml": "*",
|
"ext-xml": "*",
|
||||||
"ext-curl": "*",
|
"ext-curl": "*",
|
||||||
"ext-openssl": "*",
|
"ext-openssl": "*",
|
||||||
"ext-swoole": "*",
|
"ext-swoole": "*",
|
||||||
"ext-msgpack": "*",
|
"ext-msgpack": "*",
|
||||||
"symfony/console": "~v5.3.10",
|
"symfony/console": "^v8.0",
|
||||||
"psr/log": "1.*",
|
"psr/log": "1.*",
|
||||||
"composer-runtime-api": "^2.0",
|
"composer-runtime-api": "^2.0",
|
||||||
"psr/http-server-middleware": "1.0.1",
|
"psr/http-server-middleware": "^1.0.2",
|
||||||
"ext-pcntl": "*",
|
"ext-pcntl": "*",
|
||||||
"ext-sockets": "*",
|
"ext-sockets": "*",
|
||||||
"nikic/php-parser": "^4.15",
|
"nikic/php-parser": "^v5.5.0",
|
||||||
"ext-inotify": "*",
|
"ext-inotify": "*",
|
||||||
"game-worker/kiri-pool": "~v1.0",
|
"game-worker/kiri-pool": "^v1.0",
|
||||||
"monolog/monolog": "^2.9",
|
"psr/container": "^2.0",
|
||||||
"psr/container": "^2.0",
|
"swiftmailer/swiftmailer": "^v6.3.0"
|
||||||
"ext-libsodium": "*",
|
},
|
||||||
"swiftmailer/swiftmailer": "^6.3"
|
"replace": {
|
||||||
},
|
"symfony/polyfill-apcu": "*",
|
||||||
"replace": {
|
"symfony/polyfill-php80": "*",
|
||||||
"symfony/polyfill-apcu": "*",
|
"symfony/polyfill-mbstring": "*",
|
||||||
"symfony/polyfill-php80": "*",
|
"symfony/polyfill-ctype": "*",
|
||||||
"symfony/polyfill-mbstring": "*",
|
"symfony/polyfill-php73": "*",
|
||||||
"symfony/polyfill-ctype": "*",
|
"symfony/polyfill-php72": "*",
|
||||||
"symfony/polyfill-php73": "*",
|
"symfony/polyfill-php81": "*"
|
||||||
"symfony/polyfill-php72": "*",
|
},
|
||||||
"symfony/polyfill-php81": "*"
|
"autoload": {
|
||||||
},
|
"psr-4": {
|
||||||
"autoload": {
|
"Kiri\\": "kiri-engine/"
|
||||||
"psr-4": {
|
},
|
||||||
"Kiri\\": "kiri-engine/",
|
"files": [
|
||||||
"Kiri\\Actor\\": "kiri-actor/"
|
"Kiri.php",
|
||||||
},
|
"function.php"
|
||||||
"files": [
|
]
|
||||||
"Kiri.php",
|
}
|
||||||
"function.php"
|
}
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+780
-730
File diff suppressed because it is too large
Load Diff
@@ -1,238 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Kiri\Actor;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use JsonSerializable;
|
|
||||||
use Swoole\Coroutine;
|
|
||||||
use Swoole\Coroutine\Channel;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Actor
|
|
||||||
*/
|
|
||||||
abstract class Actor implements ActorInterface, JsonSerializable
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Channel
|
|
||||||
*/
|
|
||||||
private Channel $channel;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
private bool $isShutdown = false;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
private int $messageId = -1;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
private int $coroutineId = -1;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ActorState
|
|
||||||
*/
|
|
||||||
private ActorState $state;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var float
|
|
||||||
*/
|
|
||||||
private float $startTime = 0;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
private int $refreshInterval = 0;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ActorState
|
|
||||||
*/
|
|
||||||
public function getState(): ActorState
|
|
||||||
{
|
|
||||||
return $this->state;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ActorState $state
|
|
||||||
*/
|
|
||||||
public function setState(ActorState $state): void
|
|
||||||
{
|
|
||||||
$this->state = $state;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return float
|
|
||||||
*/
|
|
||||||
public function getRunTime(): float
|
|
||||||
{
|
|
||||||
return microtime(true) - $this->startTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $uniqueId
|
|
||||||
*/
|
|
||||||
private function __construct(readonly public string $uniqueId)
|
|
||||||
{
|
|
||||||
$this->channel = new Channel(99);
|
|
||||||
$this->startTime = microtime(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function init(): void
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isShutdown(): bool
|
|
||||||
{
|
|
||||||
return $this->isShutdown;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $id
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public static function newActor($id): static
|
|
||||||
{
|
|
||||||
$actor = new static($id);
|
|
||||||
$actor->listen();
|
|
||||||
return $actor;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function listen(): void
|
|
||||||
{
|
|
||||||
Coroutine::create(function (Actor $actor) {
|
|
||||||
$actor->coroutineId = Coroutine::getCid();
|
|
||||||
$this->run();
|
|
||||||
}, $this);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getName(): string
|
|
||||||
{
|
|
||||||
return $this->uniqueId;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $response
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function write(mixed $response): bool
|
|
||||||
{
|
|
||||||
return $this->channel->push($response);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function shutdown(): void
|
|
||||||
{
|
|
||||||
$this->isShutdown = true;
|
|
||||||
Coroutine::cancel($this->coroutineId);
|
|
||||||
if ($this->messageId > -1) {
|
|
||||||
Coroutine::cancel($this->messageId);
|
|
||||||
}
|
|
||||||
$this->channel->close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function onUpdate(): void
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function run(): void
|
|
||||||
{
|
|
||||||
if ($this->refreshInterval < 1) {
|
|
||||||
throw new Exception('Refresh interval must be greater than 1');
|
|
||||||
}
|
|
||||||
$this->setState(ActorState::BUSY);
|
|
||||||
$this->init();
|
|
||||||
$this->messageId = Coroutine::create(fn() => $this->loop());
|
|
||||||
$this->interval();
|
|
||||||
$this->setState(ActorState::IDLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function interval(): void
|
|
||||||
{
|
|
||||||
if ($this->isShutdown()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$this->onUpdate();
|
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
error($exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
Coroutine::sleep($this->refreshInterval / 1000);
|
|
||||||
|
|
||||||
$this->interval();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function loop(): bool
|
|
||||||
{
|
|
||||||
if ($this->messageId == -1) {
|
|
||||||
$this->messageId = Coroutine::getCid();
|
|
||||||
}
|
|
||||||
if ($this->channel->errCode == SWOOLE_CHANNEL_CLOSED) {
|
|
||||||
$this->channel = new Channel(99);
|
|
||||||
}
|
|
||||||
$message = $this->channel->pop();
|
|
||||||
$this->process($message);
|
|
||||||
if ($this->isShutdown()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return $this->loop();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Kiri\Actor;
|
|
||||||
|
|
||||||
interface ActorInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ActorMessage $message
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function process(ActorMessage $message): void;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Kiri\Actor;
|
|
||||||
|
|
||||||
use Swoole\Coroutine;
|
|
||||||
|
|
||||||
class ActorManager
|
|
||||||
{
|
|
||||||
|
|
||||||
/** @var array<string, ActorInterface> */
|
|
||||||
private array $nodes = [];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Actor $actor
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function addActor(ActorInterface $actor): void
|
|
||||||
{
|
|
||||||
$this->nodes[$actor->uniqueId] = $actor;
|
|
||||||
Coroutine::create(function (Actor $actor) {
|
|
||||||
$actor->run();
|
|
||||||
}, $actor);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function closeActor($name): void
|
|
||||||
{
|
|
||||||
$node = $this->nodes[$name] ?? null;
|
|
||||||
if (is_null($node)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
foreach ($node as $actor) {
|
|
||||||
$actor->shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @param $message
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function write($name, $message): bool
|
|
||||||
{
|
|
||||||
$actor = $this->nodes[$name] ?? null;
|
|
||||||
if (is_null($actor)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $actor->write($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function lists($name): array
|
|
||||||
{
|
|
||||||
$array = [];
|
|
||||||
foreach ($this->nodes[$name] as $actor) {
|
|
||||||
$array[] = [
|
|
||||||
'id' => $actor->getName(),
|
|
||||||
'state' => $actor->getState()->name,
|
|
||||||
'runTime' => $actor->getRunTime()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
return $array;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $uniqueId
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasActor(string $uniqueId): bool
|
|
||||||
{
|
|
||||||
return isset($this->nodes[$uniqueId]) && $this->nodes[$uniqueId] instanceof ActorInterface;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array|null $data
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function exec(?array $data): void
|
|
||||||
{
|
|
||||||
if (is_null($data)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function clean(): void
|
|
||||||
{
|
|
||||||
foreach ($this->nodes as $actor) {
|
|
||||||
$actor->shutdown();
|
|
||||||
}
|
|
||||||
$this->nodes = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Kiri\Actor;
|
|
||||||
|
|
||||||
use JetBrains\PhpStorm\ArrayShape;
|
|
||||||
|
|
||||||
class ActorMessage implements \JsonSerializable
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
private int $userId;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private string $event;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private array $body;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $userId
|
|
||||||
* @param string $event
|
|
||||||
* @param array $body
|
|
||||||
*/
|
|
||||||
public function __construct(int $userId, string $event, array $body)
|
|
||||||
{
|
|
||||||
$this->userId = $userId;
|
|
||||||
$this->event = $event;
|
|
||||||
$this->body = $body;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getUserId(): int
|
|
||||||
{
|
|
||||||
return $this->userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getEvent(): string
|
|
||||||
{
|
|
||||||
return $this->event;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getBody(): array
|
|
||||||
{
|
|
||||||
return $this->body;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
#[ArrayShape(['userId' => "int", 'event' => "string", 'body' => "array"])]
|
|
||||||
public function jsonSerialize(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'userId' => $this->userId,
|
|
||||||
'event' => $this->event,
|
|
||||||
'body' => $this->body
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Actor;
|
|
||||||
|
|
||||||
use Kiri\Server\Processes\AbstractProcess;
|
|
||||||
use Swoole\Coroutine;
|
|
||||||
use Swoole\Process;
|
|
||||||
|
|
||||||
class ActorProcess extends AbstractProcess
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
protected bool $enable_coroutine = true;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getName(): string
|
|
||||||
{
|
|
||||||
// TODO: Change the autogenerated stub
|
|
||||||
return 'Actor Manager';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function process(?Process $process): void
|
|
||||||
{
|
|
||||||
// TODO: Implement process() method.
|
|
||||||
while ($this->isStop() === false) {
|
|
||||||
$read = $process->read();
|
|
||||||
|
|
||||||
ActorManager::exec(json_decode($read, true));
|
|
||||||
|
|
||||||
Coroutine::sleep(1000 / 120);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function onSigterm(): void
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Actor;
|
|
||||||
|
|
||||||
enum ActorState
|
|
||||||
{
|
|
||||||
case IDLE;
|
|
||||||
case BUSY;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
case CREATE;
|
|
||||||
case MESSAGE;
|
|
||||||
case SHUTDOWN;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ abstract class BaseApplication extends LocalService
|
|||||||
*/
|
*/
|
||||||
public function parseStorage(ConfigProvider $config): void
|
public function parseStorage(ConfigProvider $config): void
|
||||||
{
|
{
|
||||||
$storage = $config->get('storage', 'storage');
|
$storage = $config->get('site.log.storage', 'storage');
|
||||||
if (!str_contains($storage, APP_PATH)) {
|
if (!str_contains($storage, APP_PATH)) {
|
||||||
$storage = APP_PATH . $storage . '/';
|
$storage = APP_PATH . $storage . '/';
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-17
@@ -10,17 +10,12 @@ declare(strict_types=1);
|
|||||||
namespace Kiri;
|
namespace Kiri;
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Kiri\Abstracts\{BaseApplication, Kernel};
|
use Kiri\Abstracts\{BaseApplication, Kernel};
|
||||||
use Kiri\Di\Scanner;
|
use Kiri\Di\Scanner;
|
||||||
use Kiri\Error\ErrorHandler;
|
use Kiri\Error\ErrorHandler;
|
||||||
use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute};
|
use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute};
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
|
||||||
use ReflectionException;
|
|
||||||
use Symfony\Component\Console\{Application as ConsoleApplication,
|
use Symfony\Component\Console\{Application as ConsoleApplication,
|
||||||
Exception\ExceptionInterface,
|
|
||||||
Input\ArgvInput,
|
Input\ArgvInput,
|
||||||
Output\ConsoleOutput,
|
Output\ConsoleOutput,
|
||||||
Output\OutputInterface
|
Output\OutputInterface
|
||||||
@@ -59,10 +54,10 @@ class Application extends BaseApplication
|
|||||||
*/
|
*/
|
||||||
public function init(): void
|
public function init(): void
|
||||||
{
|
{
|
||||||
$this->errorHandler->registerShutdownHandler(config('error.shutdown', []));
|
$this->errorHandler->registerShutdownHandler(config('site.error.shutdown', []));
|
||||||
$this->errorHandler->registerExceptionHandler(config('error.exception', []));
|
$this->errorHandler->registerExceptionHandler(config('site.error.exception', []));
|
||||||
$this->errorHandler->registerErrorHandler(config('error.error', []));
|
$this->errorHandler->registerErrorHandler(config('site.error.error', []));
|
||||||
$this->id = config('id', uniqid('id.'));
|
$this->id = config('site.id', uniqid('id.'));
|
||||||
|
|
||||||
$this->provider->on(OnBeforeCommandExecute::class, [$this, 'beforeCommandExecute']);
|
$this->provider->on(OnBeforeCommandExecute::class, [$this, 'beforeCommandExecute']);
|
||||||
}
|
}
|
||||||
@@ -75,13 +70,11 @@ class Application extends BaseApplication
|
|||||||
*/
|
*/
|
||||||
public function beforeCommandExecute(OnBeforeCommandExecute $beforeCommandExecute): void
|
public function beforeCommandExecute(OnBeforeCommandExecute $beforeCommandExecute): void
|
||||||
{
|
{
|
||||||
if (!($beforeCommandExecute->command instanceof ServerCommand)) {
|
if ($beforeCommandExecute->command instanceof ServerCommand) {
|
||||||
$scanner = $this->container->get(Scanner::class);
|
return;
|
||||||
$scanner->load_directory(APP_PATH . 'app/');
|
}
|
||||||
} else if (config('reload.hot', false) === false) {
|
$scanner = $this->container->get(Scanner::class);
|
||||||
$scanner = $this->container->get(Scanner::class);
|
$scanner->scan(APP_PATH . 'app/');
|
||||||
$scanner->load_directory(APP_PATH . 'app/');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -129,7 +122,7 @@ class Application extends BaseApplication
|
|||||||
{
|
{
|
||||||
$console = $this->container->get(ConsoleApplication::class);
|
$console = $this->container->get(ConsoleApplication::class);
|
||||||
foreach ($command as $value) {
|
foreach ($command as $value) {
|
||||||
$console->add($this->container->get($value));
|
$console->addCommand($this->container->get($value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class Coordinator
|
|||||||
|
|
||||||
const string WORKER_START = 'worker:start';
|
const string WORKER_START = 'worker:start';
|
||||||
|
|
||||||
private bool $waite = false;
|
private bool $waite = true;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,10 +17,9 @@ class Coordinator
|
|||||||
*/
|
*/
|
||||||
public function yield(): void
|
public function yield(): void
|
||||||
{
|
{
|
||||||
if ($this->waite === false) {
|
while ($this->waite) {
|
||||||
return;
|
usleep(1000);
|
||||||
}
|
}
|
||||||
$this->yield();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ class Str
|
|||||||
* @return array
|
* @return array
|
||||||
* 剩余天,带分秒
|
* 剩余天,带分秒
|
||||||
*/
|
*/
|
||||||
public static function timeout($endTime, int $startTime = NULL): array
|
public static function timeout($endTime, ?int $startTime = NULL): array
|
||||||
{
|
{
|
||||||
$endTime = $endTime - (!empty($startTime) ? $startTime : time());
|
$endTime = $endTime - (!empty($startTime) ? $startTime : time());
|
||||||
|
|
||||||
|
|||||||
@@ -11,17 +11,12 @@ namespace Kiri\Error;
|
|||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use ErrorException;
|
use ErrorException;
|
||||||
use Exception;
|
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Psr\Container\ContainerInterface;
|
|
||||||
use Kiri\Di\Inject\Container;
|
|
||||||
use ReflectionException;
|
|
||||||
use Kiri\Events\OnSystemError;
|
use Kiri\Events\OnSystemError;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ErrorHandler
|
* Class ErrorHandler
|
||||||
* hahahah
|
|
||||||
* @package Kiri\Base
|
* @package Kiri\Base
|
||||||
* @property-read $asError
|
* @property-read $asError
|
||||||
*/
|
*/
|
||||||
@@ -94,7 +89,7 @@ class ErrorHandler extends Component implements ErrorInterface
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->getLogger()->failure($lastError['message'] . PHP_EOL);
|
$this->getLogger()->println(json_encode($lastError,JSON_UNESCAPED_UNICODE));
|
||||||
|
|
||||||
event(new OnSystemError());
|
event(new OnSystemError());
|
||||||
}
|
}
|
||||||
@@ -109,7 +104,9 @@ class ErrorHandler extends Component implements ErrorInterface
|
|||||||
{
|
{
|
||||||
$this->category = 'exception';
|
$this->category = 'exception';
|
||||||
|
|
||||||
$this->getLogger()->failure($exception);
|
$this->getLogger()->println(throwable($exception));
|
||||||
|
|
||||||
|
json_log($exception);
|
||||||
|
|
||||||
event(new OnSystemError());
|
event(new OnSystemError());
|
||||||
}
|
}
|
||||||
@@ -122,6 +119,8 @@ class ErrorHandler extends Component implements ErrorInterface
|
|||||||
{
|
{
|
||||||
$error = func_get_args();
|
$error = func_get_args();
|
||||||
|
|
||||||
|
$this->getLogger()->println(json_encode($error,JSON_UNESCAPED_UNICODE));
|
||||||
|
|
||||||
event(new OnSystemError());
|
event(new OnSystemError());
|
||||||
|
|
||||||
throw new ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
|
throw new ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use Monolog\Formatter\LineFormatter;
|
|||||||
use Monolog\Handler\RotatingFileHandler;
|
use Monolog\Handler\RotatingFileHandler;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use ReflectionException;
|
use Throwable;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,7 +49,7 @@ class StdoutLogger extends Component
|
|||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->logger = new Logger(\config('id'));
|
$this->logger = new Logger(\config('site.id'));
|
||||||
$this->levels = [
|
$this->levels = [
|
||||||
'debug' => $this->logger::DEBUG,
|
'debug' => $this->logger::DEBUG,
|
||||||
'info' => $this->logger::INFO,
|
'info' => $this->logger::INFO,
|
||||||
@@ -68,31 +68,32 @@ class StdoutLogger extends Component
|
|||||||
* @param string $model
|
* @param string $model
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function failure($message, string $model = 'app'): bool
|
public function logCategory($message, string $model = 'app'): bool
|
||||||
{
|
{
|
||||||
if ($message instanceof \Exception) {
|
if ($message instanceof \Exception) {
|
||||||
$this->errors[$model] = $message->getMessage();
|
$this->errors[$model] = $message->getMessage();
|
||||||
} else {
|
} else {
|
||||||
$this->errors[$model] = $message;
|
$this->errors[$model] = $message;
|
||||||
}
|
}
|
||||||
return $this->dump($message);
|
$this->println($message);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $message
|
* @param Throwable $exception
|
||||||
* @return bool
|
* @param array $data
|
||||||
*/
|
* @param mixed|null $result
|
||||||
protected function dump($message): bool
|
* @return bool
|
||||||
{
|
*/
|
||||||
$message = throwable($message);
|
public function json_log(Throwable $exception, array $data = [], mixed $result = null): mixed
|
||||||
if (str_contains($message, 'inotify_rm_watch')) {
|
{
|
||||||
return false;
|
json_log($exception, $data);
|
||||||
}
|
|
||||||
file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $message, FILE_APPEND);
|
$this->println($exception->getMessage());
|
||||||
$this->error($message, []);
|
|
||||||
return false;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,9 +120,10 @@ class StdoutLogger extends Component
|
|||||||
} else if (method_exists($this, $name)) {
|
} else if (method_exists($this, $name)) {
|
||||||
$this->{$name}(...$arguments);
|
$this->{$name}(...$arguments);
|
||||||
}
|
}
|
||||||
} catch (\Throwable $exception) {
|
} catch (Throwable $exception) {
|
||||||
file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $exception->getMessage(), FILE_APPEND);
|
$this->println($exception->getMessage());
|
||||||
}
|
$this->json_log($exception);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,385 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2024/12/19
|
||||||
|
* Time: 14:00
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Kiri\NoSql;
|
||||||
|
|
||||||
|
use Kiri;
|
||||||
|
use Kiri\Exception\RedisConnectException;
|
||||||
|
use Kiri\Pool\Pool;
|
||||||
|
use MongoDB\Client;
|
||||||
|
use MongoDB\Database;
|
||||||
|
use MongoDB\Collection;
|
||||||
|
|
||||||
|
use function config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class MongoDB
|
||||||
|
* @package Kiri\NoSql
|
||||||
|
* @mixin Client
|
||||||
|
* @mixin Database
|
||||||
|
* @mixin Collection
|
||||||
|
*/
|
||||||
|
class MongoDB
|
||||||
|
{
|
||||||
|
|
||||||
|
public string $host = 'localhost';
|
||||||
|
public int $port = 27017;
|
||||||
|
public string $database = '';
|
||||||
|
public string $username = '';
|
||||||
|
public string $password = '';
|
||||||
|
public string $authSource = 'admin';
|
||||||
|
public int $timeout = 30;
|
||||||
|
public array $options = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array|int[]
|
||||||
|
*/
|
||||||
|
public array $pool = ['min' => 1, 'max' => 100];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
Kiri::configure($this, config('mongodb', []));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function init(): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @param $arguments
|
||||||
|
* @return mixed
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function __call($name, $arguments): mixed
|
||||||
|
{
|
||||||
|
if (method_exists($this, $name)) {
|
||||||
|
return $this->{$name}(...$arguments);
|
||||||
|
} else {
|
||||||
|
return $this->proxy($name, $arguments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取数据库实例
|
||||||
|
* @param string|null $database
|
||||||
|
* @return Database
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function getDatabase(?string $database = null): Database
|
||||||
|
{
|
||||||
|
$dbName = $database ?? $this->database;
|
||||||
|
if (empty($dbName)) {
|
||||||
|
throw new RedisConnectException('MongoDB database name is required.');
|
||||||
|
}
|
||||||
|
return $this->getClient()->selectDatabase($dbName);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取集合实例
|
||||||
|
* @param string $collection
|
||||||
|
* @param string|null $database
|
||||||
|
* @return Collection
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function getCollection(string $collection, ?string $database = null): Collection
|
||||||
|
{
|
||||||
|
return $this->getDatabase($database)->selectCollection($collection);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理方法调用到 MongoDB Client
|
||||||
|
* @param $name
|
||||||
|
* @param $arguments
|
||||||
|
* @return mixed
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function proxy($name, $arguments): mixed
|
||||||
|
{
|
||||||
|
$client = $this->getClient();
|
||||||
|
try {
|
||||||
|
// 如果方法存在于 Client,直接调用
|
||||||
|
if (method_exists($client, $name)) {
|
||||||
|
return $client->{$name}(...$arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果方法存在于 Database,通过默认数据库调用
|
||||||
|
$database = $this->getDatabase();
|
||||||
|
if (method_exists($database, $name)) {
|
||||||
|
return $database->{$name}(...$arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \BadMethodCallException("Method {$name} does not exist on MongoDB Client or Database.");
|
||||||
|
} catch (\Throwable $throwable) {
|
||||||
|
\Kiri::getLogger()->json_log($throwable);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
// MongoDB 连接是持久的,不需要释放
|
||||||
|
$this->pool()->push($this->getName(), $client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 MongoDB 命令
|
||||||
|
* @param array|object $command
|
||||||
|
* @param array $options
|
||||||
|
* @param string|null $database
|
||||||
|
* @return array|object
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function command(array|object $command, array $options = [], ?string $database = null): array|object
|
||||||
|
{
|
||||||
|
$db = $this->getDatabase($database);
|
||||||
|
return $db->command($command, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入文档
|
||||||
|
* @param string $collection
|
||||||
|
* @param array|object $document
|
||||||
|
* @param array $options
|
||||||
|
* @param string|null $database
|
||||||
|
* @return \MongoDB\InsertOneResult|\MongoDB\InsertManyResult
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function insert(string $collection, array|object $document, array $options = [], ?string $database = null)
|
||||||
|
{
|
||||||
|
$coll = $this->getCollection($collection, $database);
|
||||||
|
if (is_array($document) && isset($document[0]) && is_array($document[0])) {
|
||||||
|
// 批量插入
|
||||||
|
return $coll->insertMany($document, $options);
|
||||||
|
}
|
||||||
|
// 单条插入
|
||||||
|
return $coll->insertOne($document, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找文档
|
||||||
|
* @param string $collection
|
||||||
|
* @param array $filter
|
||||||
|
* @param array $options
|
||||||
|
* @param string|null $database
|
||||||
|
* @return \MongoDB\Driver\Cursor
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function find(string $collection, array $filter = [], array $options = [], ?string $database = null)
|
||||||
|
{
|
||||||
|
$coll = $this->getCollection($collection, $database);
|
||||||
|
return $coll->find($filter, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找单个文档
|
||||||
|
* @param string $collection
|
||||||
|
* @param array $filter
|
||||||
|
* @param array $options
|
||||||
|
* @param string|null $database
|
||||||
|
* @return array|object|null
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function findOne(string $collection, array $filter = [], array $options = [], ?string $database = null): array|object|null
|
||||||
|
{
|
||||||
|
$coll = $this->getCollection($collection, $database);
|
||||||
|
return $coll->findOne($filter, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新文档
|
||||||
|
* @param string $collection
|
||||||
|
* @param array $filter
|
||||||
|
* @param array $update
|
||||||
|
* @param array $options
|
||||||
|
* @param string|null $database
|
||||||
|
* @return \MongoDB\UpdateResult
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function update(string $collection, array $filter, array $update, array $options = [], ?string $database = null)
|
||||||
|
{
|
||||||
|
$coll = $this->getCollection($collection, $database);
|
||||||
|
if (isset($options['multi']) && $options['multi']) {
|
||||||
|
unset($options['multi']);
|
||||||
|
return $coll->updateMany($filter, $update, $options);
|
||||||
|
}
|
||||||
|
return $coll->updateOne($filter, $update, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除文档
|
||||||
|
* @param string $collection
|
||||||
|
* @param array $filter
|
||||||
|
* @param array $options
|
||||||
|
* @param string|null $database
|
||||||
|
* @return \MongoDB\DeleteResult
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function delete(string $collection, array $filter, array $options = [], ?string $database = null)
|
||||||
|
{
|
||||||
|
$coll = $this->getCollection($collection, $database);
|
||||||
|
if (isset($options['limit']) && $options['limit'] == 1) {
|
||||||
|
unset($options['limit']);
|
||||||
|
return $coll->deleteOne($filter, $options);
|
||||||
|
}
|
||||||
|
return $coll->deleteMany($filter, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统计文档数量
|
||||||
|
* @param string $collection
|
||||||
|
* @param array $filter
|
||||||
|
* @param array $options
|
||||||
|
* @param string|null $database
|
||||||
|
* @return int
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function count(string $collection, array $filter = [], array $options = [], ?string $database = null): int
|
||||||
|
{
|
||||||
|
$coll = $this->getCollection($collection, $database);
|
||||||
|
return $coll->countDocuments($filter, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function destroy(): void
|
||||||
|
{
|
||||||
|
$this->pool()->close($this->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 MongoDB 客户端
|
||||||
|
* @return Client
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
private function getClient(): Client
|
||||||
|
{
|
||||||
|
return $this->pool()->get($this->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Pool
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
protected function pool(): Pool
|
||||||
|
{
|
||||||
|
$pool = Kiri::getPool();
|
||||||
|
if (!$pool->hasChannel($this->getName())) {
|
||||||
|
$pool->created($this->getName(), $this->pool['max'], [$this, 'connect']);
|
||||||
|
}
|
||||||
|
return $pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getName(): string
|
||||||
|
{
|
||||||
|
return 'mongodb.' . $this->host . '.' . $this->database;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 MongoDB 连接
|
||||||
|
* @return Client
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
protected function connect(): Client
|
||||||
|
{
|
||||||
|
$uri = $this->buildUri();
|
||||||
|
$clientOptions = $this->buildClientOptions();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$client = new Client($uri, $clientOptions);
|
||||||
|
|
||||||
|
// 测试连接
|
||||||
|
$client->selectDatabase($this->database)->command(['ping' => 1]);
|
||||||
|
|
||||||
|
return $client;
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
\Kiri::getLogger()->json_log($e);
|
||||||
|
|
||||||
|
throw new RedisConnectException(sprintf('MongoDB Connect %s Fail: %s', $uri, $e->getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建 MongoDB 连接 URI
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function buildUri(): string
|
||||||
|
{
|
||||||
|
$auth = '';
|
||||||
|
if (!empty($this->username) && !empty($this->password)) {
|
||||||
|
$auth = $this->username . ':' . urlencode($this->password) . '@';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 支持多种 host 格式:host:port 或 host1,host2:port
|
||||||
|
$hosts = $this->host;
|
||||||
|
if (!str_contains($hosts, ',') && !str_contains($hosts, ':')) {
|
||||||
|
$hosts = $hosts . ':' . $this->port;
|
||||||
|
}
|
||||||
|
|
||||||
|
$uri = 'mongodb://' . $auth . $hosts;
|
||||||
|
|
||||||
|
// 添加数据库和认证源
|
||||||
|
$query = [];
|
||||||
|
if (!empty($this->database)) {
|
||||||
|
$query[] = 'database=' . $this->database;
|
||||||
|
}
|
||||||
|
if (!empty($this->authSource)) {
|
||||||
|
$query[] = 'authSource=' . $this->authSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($query)) {
|
||||||
|
$uri .= '/?' . implode('&', $query);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建客户端选项
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function buildClientOptions(): array
|
||||||
|
{
|
||||||
|
return array_merge([
|
||||||
|
'connectTimeoutMS' => $this->timeout * 1000,
|
||||||
|
'serverSelectionTimeoutMS' => $this->timeout * 1000,
|
||||||
|
'socketTimeoutMS' => $this->timeout * 1000,
|
||||||
|
], $this->options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -7,18 +7,15 @@
|
|||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Kiri\Redis;
|
namespace Kiri\NoSql;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Kiri\Exception\RedisConnectException;
|
use Kiri\Exception\RedisConnectException;
|
||||||
use Kiri\Pool\Pool;
|
use Kiri\Pool\Pool;
|
||||||
use RedisException;
|
|
||||||
use wchat\common\Result;
|
|
||||||
use function config;
|
use function config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Redis
|
* Class NoSql
|
||||||
* @package Kiri\Cache
|
* @package Kiri\Cache
|
||||||
* @mixin \Redis
|
* @mixin \Redis
|
||||||
*/
|
*/
|
||||||
@@ -106,13 +103,13 @@ class Redis
|
|||||||
public function lock($key, int $timeout = 5): bool|int
|
public function lock($key, int $timeout = 5): bool|int
|
||||||
{
|
{
|
||||||
$script = <<<SCRIPT
|
$script = <<<SCRIPT
|
||||||
local _nx = redis.call('setnx',KEYS[1], ARGV[1])
|
local _nx = redis.call('setnx',KEYS[1], ARGV[1])
|
||||||
if (_nx ~= 0) then
|
if (_nx ~= 0) then
|
||||||
redis.call('expire',KEYS[1], ARGV[1])
|
redis.call('expire',KEYS[1], ARGV[1])
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
SCRIPT;
|
SCRIPT;
|
||||||
return $this->eval($script, ['{lock}:' . $key, $timeout], 1);
|
return $this->eval($script, ['{lock}:' . $key, $timeout], 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +147,7 @@ SCRIPT;
|
|||||||
try {
|
try {
|
||||||
return $client->{$name}(...$arguments);
|
return $client->{$name}(...$arguments);
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
return trigger_print_error(throwable($throwable));
|
return $this->getLogger()->json_log($throwable, [], false);
|
||||||
} finally {
|
} finally {
|
||||||
if ($client->ping('h') == 'h') {
|
if ($client->ping('h') == 'h') {
|
||||||
$this->pool()->push($this->getName(), $client);
|
$this->pool()->push($this->getName(), $client);
|
||||||
@@ -159,6 +156,15 @@ SCRIPT;
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Kiri\Error\StdoutLogger
|
||||||
|
*/
|
||||||
|
protected function getLogger(): Kiri\Error\StdoutLogger
|
||||||
|
{
|
||||||
|
return Kiri::getLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Redis
|
* @return \Redis
|
||||||
* @throws
|
* @throws
|
||||||
@@ -200,10 +206,10 @@ SCRIPT;
|
|||||||
{
|
{
|
||||||
$redis = new \Redis();
|
$redis = new \Redis();
|
||||||
if (!$redis->connect($this->host, $this->port, $this->timeout)) {
|
if (!$redis->connect($this->host, $this->port, $this->timeout)) {
|
||||||
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $this->host, $this->port));
|
throw new RedisConnectException(sprintf('The NoSql Connect %s::%d Fail.', $this->host, $this->port));
|
||||||
}
|
}
|
||||||
if (!empty($this->auth) && !$redis->auth($this->auth)) {
|
if (!empty($this->auth) && !$redis->auth($this->auth)) {
|
||||||
throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $this->host, $this->auth));
|
throw new RedisConnectException(sprintf('NoSql Error: %s, Host %s, Auth %s', $redis->getLastError(), $this->host, $this->auth));
|
||||||
}
|
}
|
||||||
$redis->select($this->databases);
|
$redis->select($this->databases);
|
||||||
if ($this->read_timeout > 0) {
|
if ($this->read_timeout > 0) {
|
||||||
@@ -1 +1,25 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
use Swoole\Timer;
|
||||||
|
use Swoole\WebSocket\Server;
|
||||||
|
|
||||||
|
$client = new Server("0.0.0.0", 9511);
|
||||||
|
$client->addProcess(new Swoole\Process(function (\Swoole\Process $server) use ($client) {
|
||||||
|
while (true) {
|
||||||
|
echo json_encode($client->stats(), JSON_UNESCAPED_UNICODE) . PHP_EOL;
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
$client->on('open', function (Server $server) {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
$client->on('message', function (Server $server, $frame) {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
$client->on('close', function (Server $server, $fd) {
|
||||||
|
|
||||||
|
});
|
||||||
|
$client->start();
|
||||||
+38
-20
@@ -9,38 +9,55 @@
|
|||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
word-wrap: break-word;
|
word-wrap: break-word;
|
||||||
overflow-y: scroll;
|
overflow-y: scroll;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body style="background-color: #666;color: #fff;presentation-level: increment;">
|
<body style="background-color: #666;color: #fff;presentation-level: increment;">
|
||||||
<video id="output" width="320" height="240" autoplay></video>
|
<!--<video id="output" width="320" height="240" autoplay></video>-->
|
||||||
<pre id="format"></pre>
|
<pre id="format"></pre>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
let sock, tick, format = document.getElementById('format');
|
let sock, tick, format = document.getElementById('format');
|
||||||
|
|
||||||
|
|
||||||
let buffer;
|
// let buffer;
|
||||||
let ms = new MediaSource()
|
// let ms = new MediaSource()
|
||||||
let output = document.getElementById('output')
|
let unique = '';
|
||||||
output.src = URL.createObjectURL(ms)
|
|
||||||
ms.onsourceopen = () => {
|
// let output = document.getElementById('output')
|
||||||
buffer = ms.addSourceBuffer('video/webm; codecs="vorbis,vp8"')
|
// output.src = URL.createObjectURL(ms)
|
||||||
}
|
// ms.onsourceopen = () => {
|
||||||
|
// buffer = ms.addSourceBuffer('video/webm; codecs="vorbis,vp8"')
|
||||||
|
// }
|
||||||
|
|
||||||
function message(message) {
|
function message(message) {
|
||||||
// let div = document.createElement('div');
|
try {
|
||||||
// div.innerHTML = message.data;
|
let data = JSON.parse(message.data);
|
||||||
// div.style.cssText = 'padding:5px 10px;background-color:#222;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;word-break: break-all;word-wrap: break-word;';
|
if (data.event === 'user::kick') {
|
||||||
// div.style.marginBottom = '10px';
|
return;
|
||||||
// let count = format.getElementsByTagName('div');
|
}
|
||||||
|
if (unique === '') {
|
||||||
|
unique = message.data;
|
||||||
|
}
|
||||||
|
if (data["ack"]) {
|
||||||
|
sock.send(JSON.stringify({'event': 'ack', 'id': unique, 'data': {'ack': data["ack"]}}));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (unique === '') {
|
||||||
|
unique = message.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let div = document.createElement('div');
|
||||||
|
div.innerHTML = message.data;
|
||||||
|
div.style.cssText = 'padding:5px 10px;background-color:#222;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;word-break: break-all;word-wrap: break-word;';
|
||||||
|
div.style.marginBottom = '10px';
|
||||||
|
let count = format.getElementsByTagName('div');
|
||||||
|
|
||||||
buffer.appendBuffer(new Uint8Array(message.data))
|
format.insertBefore(div, count[0]);
|
||||||
|
|
||||||
// format.insertBefore(div, count[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function close(even) {
|
function close(even) {
|
||||||
|
unique = '';
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
connect();
|
connect();
|
||||||
sock.onmessage = message;
|
sock.onmessage = message;
|
||||||
@@ -50,13 +67,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function connect() {
|
function connect() {
|
||||||
sock = new WebSocket('wss://center-wss.stupideyes.com/ws?access_token=6648f48a-466ba-6394-70527ef8b-bc47b8');
|
sock = new WebSocket('wss://center-wss.stupideyes.com/ws?access_token=a8exu0la-77ecu-ijhk-inkgt6vln-rlwwuk');
|
||||||
sock.onopen = function () {
|
// sock = new WebSocket('wss://meet-bottle.zhuangb123.com/socket/?auth=dG9rZW49M2I2ODJhNzg0NS0xMTktMzBiMS1mMDkxOGRhNjktNTg2ZDEyJnRpbWU9MTc1MzA4MTI5MyZyZWZyZXNoPXBDT0VFYk9KOG8xTEVZQytyUkR4VlZIaXR1TmVWcndCY2crRTBua2U1ZkJuUWNJaHl6NUtTV0x2ZExXa1Y5aXlyK3NmRnRwOVRCVU91MnhPSVRPRjROTjhoT0hlODNNVmZjN1NXb2QyeDY0TXEvZTFEUCtySjNzNjZhVlplcXdYV0QzV2VRd0V6YkowZ29oOFFqRHVvZGcyb281OEZkZVp5TjVIcHFyejRZQ0VMbkxydXlCUmpFdjNTWnRsQ3gxMWthNDNxbEwzM1lJYVlaV2t3dEhOMm9VaXllNFpKOHFnU1FueEZ4N0c4RDhabzBhajFFeEJIZTlJUFQ0VUo3UkR0V0g2Y3A3bkY3bXlkVHB4Wnp5NG1kRlgxa3M5eC9iVlJHaVFDRnU4VEFsUVdDdHEzbmJ1TnNYZVd3Q2dXWEd1OEUzMld3THVFRzRCZFRCanA2MGtYUT09');
|
||||||
|
sock.onopen = function (data) {
|
||||||
if (tick) {
|
if (tick) {
|
||||||
clearInterval(tick)
|
clearInterval(tick)
|
||||||
}
|
}
|
||||||
tick = setInterval(function () {
|
tick = setInterval(function () {
|
||||||
sock.send(JSON.stringify({'route': 'getUserPosition', 'tick': new Date().getTime()}));
|
sock.send(JSON.stringify({'event': 'tick', 'id': unique, 'data': {'math': Math.ceil(Math.random() * 1000000)}}));
|
||||||
}, 3000)
|
}, 3000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,105 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use wchat\common\AppConfig;
|
||||||
|
use wchat\wx\V3\Libs\TransferDetail;
|
||||||
|
use wchat\wx\V3\Libs\TransferSceneReportInfo;
|
||||||
|
use wchat\wx\V3\WxV3Transfer;
|
||||||
|
|
||||||
|
//ini_set('memory_limit', '64G');
|
||||||
|
|
||||||
|
$msg = 'default';
|
||||||
|
try {
|
||||||
|
|
||||||
|
if (random_int(1, 10) % 3 === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$msg = 'default2';
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
var_dump($msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
//run(function () {
|
||||||
|
// $offset = 1;
|
||||||
|
// $success = 0;
|
||||||
|
//
|
||||||
|
// $group = new Coroutine\WaitGroup();
|
||||||
|
// for ($i = 0; $i < 54500; $i++) {
|
||||||
|
// $offset++;
|
||||||
|
//
|
||||||
|
// $group->add(1);
|
||||||
|
// Swoole\Coroutine::create(function () use ($offset, &$success) {
|
||||||
|
// $socket = new Swoole\Coroutine\Http\Client('192.168.0.57', 14101);
|
||||||
|
// $socket->upgrade("/websocket");
|
||||||
|
// if ($socket->connected) {
|
||||||
|
// $success += 1;
|
||||||
|
// while (true) {
|
||||||
|
// $socket->recv();
|
||||||
|
// Coroutine::sleep(0.1);
|
||||||
|
//
|
||||||
|
// $socket->push("2");
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// echo 'websocket fail: ' . socket_strerror($socket->errCode) . PHP_EOL;
|
||||||
|
// echo $success;
|
||||||
|
// }
|
||||||
|
// # echo $offset . PHP_EOL;
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// $group->wait();
|
||||||
|
//});
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//function Index_Odd()
|
||||||
|
//{
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
//class Dispatcher
|
||||||
|
//{
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// public function dispatch(){
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// public static function dispatch1()
|
||||||
|
// {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
//$t = microtime(true);
|
||||||
|
//var_dump(method_exists('Dispatcher', 'dispatch'));
|
||||||
|
//var_dump(method_exists('Dispatcher', 'dispatch1'));
|
||||||
|
//
|
||||||
|
//var_dump(microtime(true) - $t);
|
||||||
|
//
|
||||||
|
//$t = microtime(true);
|
||||||
|
//
|
||||||
|
//$r = new ReflectionClass('Dispatcher');
|
||||||
|
//
|
||||||
|
//var_dump($r->hasMethod('dispatch'));
|
||||||
|
//var_dump($r->hasMethod('dispatch1') && $r->getMethod('dispatch1')->isStatic());
|
||||||
|
//
|
||||||
|
//var_dump(microtime(true) - $t);
|
||||||
|
|
||||||
|
//$transferDetail = new TransferDetail();
|
||||||
|
//$transferDetail->setTransferAmount(1);
|
||||||
|
//$transferDetail->setTransferRemark("提现");
|
||||||
|
//$transferDetail->setTransferSceneId("1005");
|
||||||
|
//$transferDetail->setOpenid("xxxxx");
|
||||||
|
//$transferDetail->setNotifyUrl("https");
|
||||||
|
//$transferDetail->setOutBillNo("");
|
||||||
|
//$transferDetail->setTransferSceneReportInfos(new TransferSceneReportInfo('', ''), new TransferSceneReportInfo('', ''));
|
||||||
|
//$transferDetail->setUserName("");
|
||||||
|
//$transferDetail->setUserRecvPerception("");
|
||||||
|
//
|
||||||
|
//$transfer = new WxV3Transfer();
|
||||||
|
//$transfer->setPayConfig(AppConfig::instance((object)[]));
|
||||||
|
//$response = $transfer->transfer($transferDetail);
|
||||||
|
|||||||
Reference in New Issue
Block a user