Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
1、服务类不存在
2、服务方法不存在
3、自定义异常状态码和错误消息
  • Loading branch information
Tinywan committed Feb 10, 2023
1 parent 3770b4b commit ef06966
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Protocol/RpcTextProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,33 @@ class RpcTextProtocol
{
/**
* @param TcpConnection $connection
* @param $data
* @param string $data
* @return bool|null
*/
public function onMessage(TcpConnection $connection, $data)
public function onMessage(TcpConnection $connection, string $data): ?bool
{
static $instances = [];
$data = json_decode($data, true);
$class = config('plugin.tinywan.rpc.app.rpc.namespace').$data['class'];
$config = config('plugin.tinywan.rpc.app');
$class = $config['rpc']['namespace'].$data['class'];
if (!class_exists($class)) {
return $connection->send(json_encode([
'code' => $config['response']['class']['code'],
'msg' => $class. $config['response']['class']['msg']
]));
}

$method = $data['method'];
if (!method_exists($class,(string) $method)) {
return $connection->send(json_encode([
'code' => $config['response']['method']['code'],
'msg' => $method. $config['response']['method']['msg']
]));
}
$args = $data['args'];
if (!isset($instances[$class])) {
$instances[$class] = new $class();
}
$connection->send(call_user_func_array([$instances[$class], $method], $args));
return $connection->send(call_user_func_array([$instances[$class], $method], $args));
}
}
11 changes: 11 additions & 0 deletions src/config/plugin/tinywan/rpc/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,16 @@
'rpc' => [
'namespace'=> 'service\\', // 自定义服务命名空间
'listen_text_address' => 'text://0.0.0.0:9512', // 自定义Text协议地址
],
// 异常响应码和消息定义
'response' => [
'class'=> [
'code' => 404,
'msg' => '服务类不存在',
],
'method'=> [
'code' => 404,
'msg' => '服务类方法不存在',
],
]
];

0 comments on commit ef06966

Please sign in to comment.