Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Apr 2, 2024
1 parent 4de787f commit f32ebb5
Show file tree
Hide file tree
Showing 10 changed files with 842 additions and 436 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,13 @@ user_channel.trigger('client-message', {form_uid:2, content:"hello"});
#### 1. 创建连接

```php
use Workbunny\WebmanPushServer\Client;
use Workbunny\WebmanPushServer\WsClient;
use Workerman\Connection\AsyncTcpConnection;
use Workbunny\WebmanPushServer\EVENT_SUBSCRIBE;
use Workbunny\WebmanPushServer\EVENT_SUBSCRIPTION_SUCCEEDED;

// 创建连接
$client = Client::connection('127.0.0.1:8001', [
$client = WsClient::connection('127.0.0.1:8001', [
'app_key' => 'workbunny',
'heartbeat' => 60,
'auth' => 'http://127.0.0.1:8002/subscribe/auth',
Expand Down
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
"source": "https://github.com/workbunny/webman-push-server"
},
"require": {
"php": ">=8.0",
"ext-json": "*",
"ext-redis": "*",
"workerman/http-client": "^1.0 | ^2.0",
"workerman/channel": "^1.0",
"webman/console": "^1.0",
"psr/container": "^1.0 | ^2.0",
"illuminate/redis": "^9.0|^10.0",
"illuminate/database": "^9.0|^10.0",
"illuminate/events": "^9.0|^10.0",
"monolog/monolog": "^2.0 | ^3.0",
"pusher/pusher-php-server": "^7.2"
"php": ">=8.0",
"ext-json": "*",
"ext-redis": "*",
"workerman/http-client": "^1.0 | ^2.0",
"workerman/channel": "^1.0",
"webman/console": "^1.0",
"psr/container": "^1.0 | ^2.0",
"guzzlehttp/guzzle": "^7.0 | ^8.0",
"illuminate/redis": "^9.0 | ^10.0 | ^11.0",
"illuminate/database": "^9.0 | ^10.0 | ^11.0",
"illuminate/events": "^9.0 | ^10.0 | ^11.0",
"monolog/monolog": "^2.0 | ^3.0"
},
"require-dev": {
"workerman/webman-framework": "^1.5",
Expand Down
260 changes: 257 additions & 3 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,178 @@

namespace Workbunny\WebmanPushServer;

use Pusher\Pusher;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Workbunny\WebmanPushServer\Events\Subscribe;
use GuzzleHttp\Client;

class ApiClient extends Pusher
class ApiClient
{

/** @var WsClient|null */
protected Client|null $client = null;

/** @var array */
protected array $settings = [
'secret' => '',
'app_id' => '',
'base_path' => '',
'auth_key' => '',

'host' => 'http://127.0.0.1:80',
'timeout' => 30,
'keep-alive' => true
];

/**
* @param string $authKey
* @param string $secret
* @param string $appId
* @param array $options
*/
public function __construct(string $authKey, string $secret, string $appId, array $options = [])
{
$this->settings['auth_key'] = $authKey;
$this->settings['secret'] = $secret;
$this->settings['app_id'] = $appId;
$this->settings['base_path'] = '/apps/' . $this->settings['app_id'];

foreach ($options as $key => $value) {
if (isset($this->settings[$key])) {
$this->settings[$key] = $value;
}
}
$this->getClient(true);
}

/**
* 获取Client
*
* @param bool $init
* @return WsClient
*/
public function getClient(bool $init = false): Client
{
if ($init and !$this->client instanceof Client) {
$this->client = new Client([
'timeout' => $this->settings['timeout'],
'base_uri' => $this->settings['host']
]);
}
return $this->client;
}

/**
* 请求
*
* @param string $method
* @param string $path
* @param string $body
* @param array $queryParams
* @param array $headers
* @return array
* @throws ClientException
*/
public function request(string $method, string $path, string $body, array $queryParams = [], array $headers = []): array
{
$path = $this->settings['base_path'] . $path;
$queryParams['body_md5'] = md5($body);
try {
$response = $this->getClient()->request($method, $path, [
RequestOptions::QUERY => $this->sign($path, $method, $queryParams),
RequestOptions::JSON => $body,
RequestOptions::HEADERS => [
'Content-Type' => 'application/json',
'Connection' => $this->settings['keep-alive'] ? 'keep-alive' : 'close',
'X-Push-Client' => 'push-server ' . VERSION
] + $headers,
RequestOptions::HTTP_ERRORS => false,
]);
$status = $response->getStatusCode();
$body = $response->getBody()->getContents();
if (
$status === 200 or
($status >= 400 and $status < 500)
) {
return @json_decode($body, true) ?: [];
}
} catch (GuzzleException $e) {
$body = $e->getMessage();
$status = $e->getCode();
}
throw new ClientException($body, $status);
}

/**
* @param string $channel
* @param array $params
* @return array
* @throws ClientException
*/
public function getChannelInfo(string $channel, array $params = []): array
{
return $this->request('GET','/channels/' . $channel, '', $params);
}

/**
* @param array $params
* @return array
* @throws ClientException
*/
public function getChannels(array $params = []): array
{
return $this->request('GET', '/channels', '', $params);
}

/**
* @param string $channel
* @return array
*/
public function getPresenceUsers(string $channel): array
{
return $this->request('GET', "/channels/$channel/users", '');
}

/**
* @param array $channels
* @param string $event
* @param $data
* @param array $params
* @return array
*/
public function trigger(array $channels, string $event, $data, array $params = []): array
{
$socketId = $params['socket_id'] ?? null;
unset($params['socket_id']);
return $this->request('POST', '/events', json_encode([
'channels' => $channels,
'name' => $event,
'data' => $data,
'socket_id' => $socketId
],JSON_UNESCAPED_UNICODE), $params);
}

/**
* @param array $batch
* @param array $params
* @return array
*/
public function triggerBatch(array $batch, array $params = []): array
{
return $this->request('POST', '/events', json_encode([
'batch' => $batch,
],JSON_UNESCAPED_UNICODE), $params);
}

/**
* @param string $userId
* @return array
*/
public function terminateUserConnections(string $userId): array
{
return $this->request('POST', "/users/$userId/terminate_connections", '');
}

/**
* @param string $appKey
* @param string $appSecret
Expand All @@ -39,7 +206,94 @@ public static function subscribeAuth(string $appKey, string $appSecret, string $
* @param array $query
* @return mixed
*/
public static function routeAuth(string $appKey, string $appSecret, string $httpMethod, string $httpPath, array $query){
public static function routeAuth(string $appKey, string $appSecret, string $httpMethod, string $httpPath, array $query): mixed
{
return Server::isDebug() ? 'test' : self::build_auth_query_params($appKey, $appSecret, $httpMethod, $httpPath, $query)['auth_signature'];
}

/**
* Build the required HMAC'd auth string.
*
* @param string $auth_key
* @param string $auth_secret
* @param string $request_method
* @param string $request_path
* @param array $query_params [optional]
* @param string $auth_version [optional]
* @param string|null $auth_timestamp [optional]
* @return array
*/
public static function build_auth_query_params(
string $auth_key,
string $auth_secret,
string $request_method,
string $request_path,
array $query_params = [],
string $auth_version = '1.0',
string $auth_timestamp = null
): array {
$params = [];
$params['auth_key'] = $auth_key;
$params['auth_timestamp'] = (is_null($auth_timestamp) ? time() : $auth_timestamp);
$params['auth_version'] = $auth_version;

$params = array_merge($params, $query_params);
ksort($params);

$string_to_sign = "$request_method\n" . $request_path . "\n" . self::array_implode('=', '&', $params);

$auth_signature = hash_hmac('sha256', $string_to_sign, $auth_secret, false);

$params['auth_signature'] = $auth_signature;

return $params;
}

/**
* Implode an array with the key and value pair giving
* a glue, a separator between pairs and the array
* to implode.
*
* @param string $glue The glue between key and value
* @param string $separator Separator between pairs
* @param array|string $array The array to implode
*
* @return string The imploded array
*/
public static function array_implode(string $glue, string $separator, $array): string
{
if (!is_array($array)) {
return $array;
}

$string = [];
foreach ($array as $key => $val) {
if (is_array($val)) {
$val = implode(',', $val);
}
$string[] = "{$key}{$glue}{$val}";
}

return implode($separator, $string);
}

/**
* Utility function used to generate signing headers
*
* @param string $path
* @param string $request_method
* @param array $query_params [optional]
*
* @return array
*/
private function sign(string $path, string $request_method = 'GET', array $query_params = []): array
{
return self::build_auth_query_params(
$this->settings['auth_key'],
$this->settings['secret'],
$request_method,
$path,
$query_params
);
}
}
Loading

0 comments on commit f32ebb5

Please sign in to comment.