• Модуль: pull
  • Путь к файлу: ~/bitrix/modules/pull/lib/protobuftransport.php
  • Класс: BitrixPullProtobufTransport
  • Вызов: ProtobufTransport::getOnlineChannels
static function getOnlineChannels(array $channels)
{
	$result = [];
	$maxChannelsPerRequest = CPullOptions::GetMaxChannelsPerRequest();
	$channelBatches = [];
	$currentChannelBatch = 0;
	$requestsInChannelBatch = 0;
	foreach ($channels as $channelId)
	{
		$channel = new ProtobufChannelId();
		$channel->setId(hex2bin($channelId));
		$channel->setIsPrivate(true);

		$requestsInChannelBatch++;

		if($requestsInChannelBatch >= $maxChannelsPerRequest)
		{
			$currentChannelBatch++;
			$requestsInChannelBatch = 1;
		}
		$channelBatches[$currentChannelBatch][] = $channel;
	}

	$requests = [];
	foreach ($channelBatches as $channelBatchNumber => $channelBatch)
	{
		$channelsStatsRequest = new ProtobufChannelStatsRequest();
		$channelsStatsRequest->setChannelsList(new MessageCollection($channelBatch));

		$request = new ProtobufRequest();
		$request->setChannelStats($channelsStatsRequest);
		$requests[] = $request;
	}

	$queueServerUrl = CHTTP::urlAddParams(Config::getPublishUrl(), ["binaryMode" => "true"]);

	$requestBatches = static::createRequestBatches($requests);
	foreach ($requestBatches as $requestBatch)
	{
		$http = new HttpClient();
		$http->disableSslVerification();

		$urlWithSignature = $queueServerUrl;
		$bodyStream = $requestBatch->toStream();
		if(CPullOptions::IsServerShared())
		{
			$signature = CPullChannel::GetSignature($bodyStream->getContents());
			$urlWithSignature = CHTTP::urlAddParams($urlWithSignature, ["signature" => $signature]);
		}

		$binaryResponse = $http->post($urlWithSignature, $bodyStream);

		if($http->getStatus() != 200)
		{
			return [];
		}
		if(strlen($binaryResponse) == 0)
		{
			return [];
		}

		try
		{
			$responseBatch = ProtobufResponseBatch::fromStream($binaryResponse);
		}
		catch (Exception $e)
		{
			return [];
		}
		$responses = $responseBatch->getResponsesList();

		$response = $responses[0];
		if(!($response instanceof ProtobufResponse))
		{
			return[];
		}

		if ($response->hasChannelStats())
		{
			$stats = $response->getChannelStats();
			/** @var ProtobufChannelStats $channel */
			foreach ($stats->getChannelsList() as $channel)
			{
				if($channel->getIsOnline())
				{
					$channelId = bin2hex($channel->getId());
					$result[$channelId] = true;
				}
			}
		}
	}

	return $result;
}