https://github.com/cloudtay/ripple-rpc
https://github.com/cloudtay/ripple-rpc
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/cloudtay/ripple-rpc
- Owner: cloudtay
- License: mit
- Created: 2025-02-11T06:55:34.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-11T10:15:43.000Z (over 1 year ago)
- Last Synced: 2025-09-18T12:34:25.110Z (9 months ago)
- Language: PHP
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## 简介
本项目是一个基于`ripple`引擎实现的PHP服务器,支持`HTTP`和`WebSocket`通信方式
### 特性
* 支持`JSON-RPC 2.0`
* 同时支持 `HTTP` 与 `WebSocket` 协议
* 可自定义路由
* 支持中间件
* 错误处理与异常捕获
## 快速开始
### 安装
```bash
composer require cloudtay/ripple-rpc
```
### 创建服务器实例
```php
bind('http://0.0.0.0:8000/jsonrpc');
// 或者绑定WebSocket服务地址
$server->bind('ws://0.0.0.0:9000');
// 注册路由
$server->route('sum', static function (int $a, int $b) {
return $a + $b;
});
// 支持callable数组
$server->route('createFromFormat', [DateTime::class, 'createFromFormat']);
$server->run();
// 添加中间件
$server->middleware(static function ($next) {
// 通过上下文获取请求信息(仅在HTTP协议下有效)
$request = Context::get('request');
// 通过上下文获取连接信息(仅在WebSocket协议下有效)
$connection = Context::get('connection');
// 通过上下文获取请求JSON
$requestJson = Context::get('requestJson');
if (\rand(0, 1) === 1) {
// 自定义错误处理
// throw new JsonException([
// 'code' => -32603,
// 'message' => '服务器内部错误',
// ]);
}
return $next();
});
// 运行服务器
$server->run();
// 等待协程结束
wait();
```
### 客户端调用
```php