Commit f0ed7539 authored by 杨培's avatar 杨培

增加请求与返回ID匹配校验;优化RPC服务调用异常处理;

parent 78b08abb
......@@ -16,18 +16,23 @@ Message::setDebug();
//$ret = Message::sendTemplateSmsByUidAsync('12', 'default_code', ['code'=>rand(1000, 9999)]);
//$ret = Message::sendSms('18019580609', 'Hello World 3');
$ret = Message::sendTemplateSms('18019580609', 'shop_item_shipped', [
'order' => '4124',
'express' => '顺丰速运',
'number' => 'sf123456'
]);
try {
$ret = Message::sendTemplateSmsAsync('18019580609', 'shop_item_shipped', [
'order' => '4124',
'express' => '顺丰速运',
'number' => 'sf123456'
]);
} catch (\Exception $e) {
echo $e->getCode().'/'.$e->getMessage();
$ret = null;
}
//Message::setUserOnline(12, 60);
//$ret = Message::isUseOnline(12);
$ret = Message::sendSystemMessageAsync('remind', 0, 12, Message::TYPE_REMIND, [
'title' => '测试的标题',
'text'=>'新消息测试'.uniqid()
]);
//$ret = Message::sendSystemMessageAsync('remind', 0, 12, Message::TYPE_REMIND, [
// 'title' => '测试的标题',
// 'text'=>'新消息测试'.uniqid()
//]);
var_dump($ret);
\ No newline at end of file
......@@ -217,7 +217,7 @@ class Client
]);
if (!$conn->connect()) {
throw new ClientException("Unable to connect to rpc server. {$srv['host']}:{$srv['port']}");
throw new ClientException("Unable to connect to RPC server {$srv['host']}:{$srv['port']}");
}
$connections[$instance->rpcServer] = $conn;
......
......@@ -194,13 +194,13 @@ class JsonRpc
}
}
public function invoke($method, $params, $id=null)
public function invoke($method, $params)
{
$data = [
'jsonrpc' => '2.0',
'method' => $method,
'params' => $params,
'id' => $id
'id' => uniqid()
];
$json = json_encode($data);
......@@ -213,9 +213,14 @@ class JsonRpc
$ret = json_decode($response, true);
if ($ret['id'] != $data['id']) {
$this->_error('Request id not matched.');
throw new ApiException('Request id not matched.');
}
if (!empty($ret['error'])) {
$this->_error($ret['error']['message']);
throw new ApiException($ret['error']['message'], $ret['error']['code']);
throw new ApiException($ret['error']['message'], $ret['error']['data']['code'] ?? 0);
}
return $ret['result'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment