-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from jjok/client-factory
Add client factory
- Loading branch information
Showing
5 changed files
with
94 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$config = include('config.php'); | ||
use oliverlorenz\reactphpmqtt\ClientFactory; | ||
use oliverlorenz\reactphpmqtt\protocol\Version4; | ||
use React\Stream\Stream; | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$dnsResolverFactory = new React\Dns\Resolver\Factory(); | ||
$resolver = $dnsResolverFactory->createCached('8.8.8.8', $loop); | ||
$config = require 'config.php'; | ||
|
||
$version = new oliverlorenz\reactphpmqtt\protocol\Version4(); | ||
$connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version); | ||
$connector = ClientFactory::createClient(new Version4(), '8.8.8.8'); | ||
|
||
$p = $connector->create($config['server'], $config['port'], $config['options']); | ||
$p->then(function(\React\Stream\Stream $stream) use ($connector) { | ||
$p->then(function(Stream $stream) use ($connector) { | ||
return $connector->publish($stream, 'hello/world', 'example message'); | ||
}); | ||
|
||
$loop->run(); | ||
$connector->getLoop()->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace oliverlorenz\reactphpmqtt; | ||
|
||
use oliverlorenz\reactphpmqtt\protocol\Version; | ||
use React\Dns\Resolver\Factory as DnsResolverFactory; | ||
use React\EventLoop\Factory as EventLoopFactory; | ||
|
||
class ClientFactory | ||
{ | ||
public static function createClient(Version $version, $resolverIp = '8.8.8.8') | ||
{ | ||
$loop = EventLoopFactory::create(); | ||
$resolver = self::createDnsResolver($resolverIp, $loop); | ||
|
||
return new Connector($loop, $resolver, $version); | ||
} | ||
|
||
public static function createSecureClient(Version $version, $resolverIp = '8.8.8.8') | ||
{ | ||
$loop = EventLoopFactory::create(); | ||
$resolver = self::createDnsResolver($resolverIp, $loop); | ||
|
||
return new SecureConnector($loop, $resolver, $version); | ||
} | ||
|
||
private static function createDnsResolver($resolverIp, $loop) | ||
{ | ||
$dnsResolverFactory = new DnsResolverFactory(); | ||
|
||
return $dnsResolverFactory->createCached($resolverIp, $loop); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace oliverlorenz\reactphpmqtt; | ||
|
||
use oliverlorenz\reactphpmqtt\protocol\Version4; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
/** | ||
* @covers \oliverlorenz\reactphpmqtt\ClientFactory | ||
*/ | ||
final class ClientFactoryTest extends TestCase | ||
{ | ||
public function testConnectorCanBeCreated() | ||
{ | ||
$client = ClientFactory::createClient(new Version4(), '8.8.8.8'); | ||
|
||
$this->assertInstanceOf('oliverlorenz\reactphpmqtt\Connector', $client); | ||
} | ||
|
||
public function testSecureConnectorCanBeCreated() | ||
{ | ||
$client = ClientFactory::createSecureClient(new Version4(), '8.8.8.8'); | ||
|
||
$this->assertInstanceOf('oliverlorenz\reactphpmqtt\SecureConnector', $client); | ||
} | ||
} |