valga / fbns-react
基于ReactPHP构建的FBNS PHP客户端
0.3.1
2020-07-05 10:21 UTC
Requires
- php: ^7.2
- ext-json: *
- ext-mbstring: *
- ext-zlib: *
- binsoul/net-mqtt: ^0.8
- binsoul/net-mqtt-client-react: ^0.7.2
- evenement/evenement: ~2.0|~3.0
- psr/log: ~1.0
- ramsey/uuid: ^3.9 | ^4.0
- react/event-loop: ^1.1
- react/promise: ^2.7
- react/socket: ^1.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- monolog/monolog: ~1.23
- phpunit/phpunit: ^8.5.2
Suggests
- ext-event: For more efficient event loop implementation.
- ext-gmp: To be able to run this code on x86 PHP builds.
README
FBNS的PHP客户端,基于ReactPHP构建。包含通用的RTI客户端实现。
要求
您必须安装GMP扩展,才能在x86 PHP构建上运行此代码。
安装
composer require valga/fbns-react
基本用法
// Set up a Push client. $loop = \React\EventLoop\Factory::create(); $auth = new \Fbns\Auth\DeviceAuth(); $device = new \Fbns\Device\DefaultDevice(USER_AGENT); $network = new \Fbns\Network\Wifi(); $client = new \Fbns\Client($loop, $auth, $device, $network, $logger); // Read saved credentials from the storage. try { $auth->read($storage->get('fbns_auth')); } catch (\Throwable $e) { } // Bind events. $client ->on('connect', static function (string $jsonAuth) use ($client, $auth, $storage, $app) { // Update credentials and save them to the storage for future use. try { $auth->read($jsonAuth); $storage->set('fbns_auth', json_encode($auth)); } catch (\Throwable $e) { } // Register the application. $client->register(PACKAGE_NAME, APPLICATION_ID) ->then(static function (\Fbns\Push\Registration $registration) use ($app) { $app->registerPushToken($registration->getToken()); }); }) ->on('push', static function (\Fbns\Push\Notification $message) use ($app) { // Handle received notification payload. $app->handlePushNotification($message->getPayload()); }); // Connect to the broker. $client->connect(HOSTNAME, PORT); // Run main loop. $loop->run();
高级用法
// Set up a proxy. $connector = new \React\Socket\Connector($loop); $proxy = new \Clue\React\HttpProxy('username:password@127.0.0.1:3128', $connector); // Disable SSL verification. $ssl = new \React\Socket\SecureConnector($proxy, $loop, ['verify_peer' => false, 'verify_peer_name' => false]); // Enable logging to stdout. $logger = new \Monolog\Logger('push'); $logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::INFO)); // Set up a Push client. $client = new \Fbns\Client($loop, $auth, $device, $network, $logger, $connector); // Persistence. $client->on('disconnect', static function () { // Network connection has been closed. You can reestablish it if you want to. }); $client->connect(HOSTNAME, PORT) ->otherwise(static function () { // Connection attempt was unsuccessful, retry with an exponential backoff. });