1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
jsonrpctransport.php
См. документацию.
1<?php
2
3namespace Bitrix\Pull;
4
5use Bitrix\Main;
6
8{
9 protected const VERSION = '2.0';
10 protected const METHOD_PUBLISH = 'publish';
11 protected const METHOD_GET_LAST_SEEN = 'getUsersLastSeen';
12 protected const METHOD_UPDATE_LAST_SEEN = 'updateUsersLastSeen';
13
14 protected string $serverUrl = '';
15 protected string $hostname = '';
16
17 function __construct(array $options = [])
18 {
19 $this->serverUrl = $options['serverUrl'] ?? Config::getJsonRpcUrl();
20 $this->hostname = $options['hostname'] ?? Config::getHostname();
21 }
22
30 {
32 $result->withRemoteAddress($this->serverUrl);
33 try
34 {
35 $batchList = static::createRequestBatches($messages);
36 }
37 catch (\Throwable $e)
38 {
39 return $result->addError(new \Bitrix\Main\Error($e->getMessage(), $e->getCode()));
40 }
41
42 foreach ($batchList as $batch)
43 {
44 $executeResult = $this->executeBatch($this->serverUrl, $batch);
45 if (!$executeResult->isSuccess())
46 {
47 return $result->addErrors($executeResult->getErrors());
48 }
49 }
50
51 return $result;
52 }
53
55 {
56 $rpcResult = $this->executeMethod(
57 $this->serverUrl,
58 static::METHOD_GET_LAST_SEEN,
59 [
60 'userList' => $userList
61 ]
62 );
63
64 if (!$rpcResult->isSuccess())
65 {
66 return $rpcResult;
67 }
68
69 $response = $rpcResult->getData();
70 $data = is_array($response['result']) ? $response['result'] : [];
71 $result = new Main\Result();
72
73 return $result->setData($data);
74 }
75
82 public function updateUsersLastSeen(array $userTimestamps): Main\Result
83 {
84 return $this->executeMethod(
85 $this->serverUrl,
86 static::METHOD_UPDATE_LAST_SEEN,
87 $userTimestamps
88 );
89 }
90
95 protected static function createRequestBatches(array $messages): array
96 {
97 // creates just one batch right now
98 $maxPayload = \CPullOptions::GetMaxPayload() - 20;
99
100 $result = [];
101 $currentBatch = [];
102 $currentBatchSize = 2; // opening and closing bracket
103 foreach ($messages as $message)
104 {
105 $message->userList = array_values($message->userList);
106 $message->channelList = array_values($message->channelList);
107 $jsonRpcMessage = Main\Web\Json::encode(static::createJsonRpcRequest(static::METHOD_PUBLISH, $message));
108 if (mb_strlen($jsonRpcMessage) > $maxPayload - 20)
109 {
110 trigger_error("Pull message exceeds size limit, skipping", E_USER_WARNING);
111 }
112 if (($currentBatchSize + mb_strlen($jsonRpcMessage)) + 1> $maxPayload)
113 {
114 // start new batch
115 $result[] = "[" . implode(",", $currentBatch) . "]";
116 $currentBatch = [];
117 $currentBatchSize = 2;
118 }
119 $currentBatch[] = $jsonRpcMessage;
120 $currentBatchSize += (mb_strlen($jsonRpcMessage)) + 1; // + comma
121 }
122 if (count($currentBatch) > 0)
123 {
124 $result[] = "[" . implode(",", $currentBatch) . "]";
125 }
126 return $result;
127 }
128
134 protected static function createJsonRpcRequest(string $method, $params): array
135 {
136 return [
137 'jsonrpc' => static::VERSION,
138 'method' => $method,
139 'params' => $params
140 ];
141 }
142
143 protected function executeMethod(string $queueServerUrl, string $method, array $params): Main\Result
144 {
145 $result = new Main\Result();
146 $rpcRequest = static::createJsonRpcRequest($method, $params);
147
148 try
149 {
150 $body = Main\Web\Json::encode($rpcRequest);
151 }
152 catch (\Throwable $e)
153 {
154 return $result->addError(new \Bitrix\Main\Error($e->getMessage(), $e->getCode()));
155 }
156 $httpResult = $this->performHttpRequest($queueServerUrl, $body);
157 if (!$httpResult->isSuccess())
158 {
159 return $result->addErrors($httpResult->getErrors());
160 }
161 $response = $httpResult->getData();
162 if (!isset($response['jsonrpc']) || $response['jsonrpc'] != static::VERSION)
163 {
164 return $result->addError(new \Bitrix\Main\Error('Wrong response structure'));
165 }
166 if (is_array($response['error']))
167 {
168 return $result->addError(new \Bitrix\Main\Error($response['error']['message'], $response['error']['code']));
169 }
170
171 return $result->setData($response);
172 }
173
174 protected function executeBatch(string $queueServerUrl, string $batchBody): Main\Result
175 {
176 $result = new Main\Result();
177 $httpResult = $this->performHttpRequest($queueServerUrl, $batchBody);
178 if (!$httpResult->isSuccess())
179 {
180 return $result->addErrors($httpResult->getErrors());
181 }
182 $response = $result->getData();
183
184 return $result->setData($response);
185 }
186
187 protected function performHttpRequest(string $queueServerUrl, string $body): Main\Result
188 {
189 $result = new Main\Result();
190 $httpClient = new Main\Web\HttpClient(["streamTimeout" => 1]);
191
192 $signature = \CPullChannel::GetSignature($body);
193 $hostId = (string)Config::getHostId();
194 $additionalParams = ["hostId" => $hostId, "signature" => $signature];
195 if ($this->hostname != '')
196 {
197 $additionalParams['hostname'] = $this->hostname;
198 }
199 $urlWithSignature = \CHTTP::urlAddParams($queueServerUrl, $additionalParams);
200
201 $sendResult = $httpClient->query(Main\Web\HttpClient::HTTP_POST, $urlWithSignature, $body);
202 if (!$sendResult)
203 {
204 $errorCode = array_key_first($httpClient->getError());
205 $errorMsg = $httpClient->getError()[$errorCode];
206 return $result->addError(new Main\Error($errorMsg, $errorCode));
207 }
208 $responseCode = (int)$httpClient->getStatus();
209 if ($responseCode !== 200)
210 {
211 return $result->addError(new Main\Error("Unexpected server response code {$responseCode}"));
212 }
213 $responseBody = $httpClient->getResult();
214 if ($responseBody == '')
215 {
216 return $result->addError(new Main\Error('Empty server response'));
217 }
218 try
219 {
220 $decodedBody = Main\Web\Json::decode($responseBody);
221 }
222 catch (\Throwable $e)
223 {
224 return $result->addError(new Main\Error('Could not decode server response. Raw response: ' . $responseBody));
225 }
226
227 return $result->setData($decodedBody);
228 }
229}
Определения error.php:15
static decode($data)
Определения json.php:50
static encode($data, $options=null)
Определения json.php:22
sendMessages(array $messages)
Определения jsonrpctransport.php:29
__construct(array $options=[])
Определения jsonrpctransport.php:17
const METHOD_GET_LAST_SEEN
Определения jsonrpctransport.php:11
static createJsonRpcRequest(string $method, $params)
Определения jsonrpctransport.php:134
executeMethod(string $queueServerUrl, string $method, array $params)
Определения jsonrpctransport.php:143
const METHOD_UPDATE_LAST_SEEN
Определения jsonrpctransport.php:12
getUsersLastSeen(array $userList)
Определения jsonrpctransport.php:54
performHttpRequest(string $queueServerUrl, string $body)
Определения jsonrpctransport.php:187
updateUsersLastSeen(array $userTimestamps)
Определения jsonrpctransport.php:82
static createRequestBatches(array $messages)
Определения jsonrpctransport.php:95
executeBatch(string $queueServerUrl, string $batchBody)
Определения jsonrpctransport.php:174
static urlAddParams($url, $add_params, $options=[])
Определения http.php:521
static GetSignature($value, $signatureKey=null)
Определения pull_channel.php:212
$options
Определения commerceml2.php:49
$data['IS_AVAILABLE']
Определения .description.php:13
$userList
Определения discount_coupon_list.php:276
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
Определения cookie.php:3
$message
Определения payment.php:8
$errorMsg
Определения refund.php:16
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$messages
Определения template.php:8
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$response
Определения result.php:21
$method
Определения index.php:27