enterness / fbns-react
基于ReactPHP构建的FBNS PHP客户端
0.1.8
2017-10-09 07:54 UTC
Requires
- php: ~5.6|~7.0
- ext-mbstring: *
- ext-zlib: *
- binsoul/net-mqtt: ~0.2
- evenement/evenement: ~2.0|~3.0
- psr/log: ~1.0
- react/event-loop: ^0.4.3
- react/promise: ~2.0
- react/socket: ~0.8
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.4
- monolog/monolog: ~1.23
Suggests
- ext-event: For more efficient event loop implementation.
- ext-gmp: To be able to run this code on x86 PHP builds.
README
基于ReactPHP构建的FBNS PHP客户端。
要求
您需要安装GMP扩展,才能在x86 PHP构建上运行此代码。
安装
composer require valga/fbns-react
基本用法
// Set up a FBNS client. $loop = \React\EventLoop\Factory::create(); $client = new \Fbns\Client\Lite($loop); // Read saved credentials from a storage. $auth = new \Fbns\Client\Auth\DeviceAuth(); try { $auth->read($storage->get('fbns_auth')); } catch (\Exception $e) { } // Connect to a broker. $connection = new \Fbns\Client\Connection($deviceAuth, USER_AGENT); $client->connect(HOSTNAME, PORT, $connection); // Bind events. $client ->on('connect', function (\Fbns\Client\Lite\ConnectResponsePacket $responsePacket) use ($client, $auth, $storage) { // Update credentials and save them to a storage for future use. try { $auth->read($responsePacket->getAuth()); $storage->set('fbns_auth', $responsePacket->getAuth()); } catch (\Exception $e) { } // Register an application. $client->register(PACKAGE_NAME, APPLICATION_ID); }) ->on('register', function (\Fbns\Client\Message\Register $message) use ($app) { // Register received token with an application. $app->registerPushToken($message->getToken()); }) ->on('push', function (\Fbns\Client\Message\Push $message) use ($app) { // Handle received notification payload. $app->handlePushNotification($message->getPayload()); }); // 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('fbns'); $logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::INFO)); // Set up a client. $client = new \Fbns\Client\Lite($loop, $ssl, $logger); // Persistence. $client->on('disconnect', function () { // Network connection has been closed. You can reestablish it if you want to. }); $client->connect(HOSTNAME, PORT, $connection) ->otherwise(function () { // Connection attempt was unsuccessful, retry with an exponential backoff. });