Skip to content

Commit a7614ee

Browse files
committed
Update to SocketClient v0.6+ and use connect($uri)
1 parent 5170c27 commit a7614ee

21 files changed

+107
-101
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ In order to be able to establish several connections at the same time, [react/so
3131
API to establish simple connections in an async (non-blocking) way.
3232

3333
This project includes several classes that extend this base functionality by implementing the same simple `ConnectorInterface`.
34-
This interface provides a single promise-based method `create($host, $ip)` which can be used to easily notify
34+
This interface provides a single promise-based method `connect($uri)` which can be used to easily notify
3535
when the connection is successfully established or the `Connector` gives up and the connection fails.
3636

3737
```php
38-
$connector->create('www.google.com', 80)->then(function ($stream) {
38+
$connector->connect('www.google.com:80')->then(function ($stream) {
3939
echo 'connection successfully established';
4040
$stream->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
4141
$stream->end();
@@ -70,7 +70,7 @@ and then retry up to 2 times if the connection attempt fails:
7070
```php
7171
$connectorRepeater = new ConnectionManagerRepeat($connector, 3);
7272

73-
$connectorRepeater->create('www.google.com', 80)->then(function ($stream) {
73+
$connectorRepeater->connect('www.google.com:80')->then(function ($stream) {
7474
echo 'connection successfully established';
7575
$stream->close();
7676
});

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"require": {
1717
"php": ">=5.3",
18-
"react/socket-client": "^0.5 || ^0.4 || ^0.3",
18+
"react/socket-client": "^0.7 || ^0.6",
1919
"react/event-loop": "^0.4 || ^0.3",
2020
"react/promise": "^2.1 || ^1.2",
2121
"react/promise-timer": "^1.1"

src/ConnectionManagerDelay.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public function __construct(ConnectorInterface $connectionManager, $delay, LoopI
1919
$this->loop = $loop;
2020
}
2121

22-
public function create($host, $port)
22+
public function connect($uri)
2323
{
2424
$connectionManager = $this->connectionManager;
2525

26-
return Timer\resolve($this->delay, $this->loop)->then(function () use ($connectionManager, $host, $port) {
27-
return $connectionManager->create($host, $port);
26+
return Timer\resolve($this->delay, $this->loop)->then(function () use ($connectionManager, $uri) {
27+
return $connectionManager->connect($uri);
2828
});
2929
}
3030
}

src/ConnectionManagerReject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// a simple connection manager that rejects every single connection attempt
1010
class ConnectionManagerReject implements ConnectorInterface
1111
{
12-
public function create($host, $port)
12+
public function connect($_)
1313
{
1414
return Promise\reject(new Exception('Connection rejected'));
1515
}

src/ConnectionManagerRepeat.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ public function __construct(ConnectorInterface $connectionManager, $maximumTries
2222
$this->maximumTries = $maximumTries;
2323
}
2424

25-
public function create($host, $port)
25+
public function connect($uri)
2626
{
2727
$tries = $this->maximumTries;
2828
$connector = $this->connectionManager;
2929

30-
return new Promise(function ($resolve, $reject) use ($host, $port, &$pending, &$tries, $connector) {
31-
$try = function ($error = null) use (&$try, &$pending, &$tries, $host, $port, $connector, $resolve, $reject) {
30+
return new Promise(function ($resolve, $reject) use ($uri, &$pending, &$tries, $connector) {
31+
$try = function ($error = null) use (&$try, &$pending, &$tries, $uri, $connector, $resolve, $reject) {
3232
if ($tries > 0) {
3333
--$tries;
34-
$pending = $connector->create($host, $port);
34+
$pending = $connector->connect($uri);
3535
$pending->then($resolve, $try);
3636
} else {
3737
$reject(new Exception('Connection still fails even after retrying', 0, $error));

src/ConnectionManagerSwappable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public function __construct(ConnectorInterface $connectionManager)
1414
$this->connectionManager = $connectionManager;
1515
}
1616

17-
public function create($host, $port)
17+
public function connect($uri)
1818
{
19-
return $this->connectionManager->create($host, $port);
19+
return $this->connectionManager->connect($uri);
2020
}
2121

2222
public function setConnectionManager(ConnectorInterface $connectionManager)

src/ConnectionManagerTimeout.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public function __construct(ConnectorInterface $connectionManager, $timeout, Loo
2020
$this->loop = $loop;
2121
}
2222

23-
public function create($host, $port)
23+
public function connect($uri)
2424
{
25-
$promise = $this->connectionManager->create($host, $port);
25+
$promise = $this->connectionManager->connect($uri);
2626

2727
return Timer\timeout($promise, $this->timeout, $this->loop)->then(null, function ($e) use ($promise) {
2828
// connection successfully established but timeout already expired => close successful connection

src/Multiple/ConnectionManagerConcurrent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
class ConnectionManagerConcurrent extends ConnectionManagerConsecutive
1010
{
11-
public function create($host, $port)
11+
public function connect($uri)
1212
{
1313
$all = array();
1414
foreach ($this->managers as $connector) {
1515
/* @var $connection Connector */
16-
$all []= $connector->create($host, $port);
16+
$all []= $connector->connect($uri);
1717
}
1818
return Promise\any($all)->then(function ($conn) use ($all) {
1919
// a connection attempt succeeded

src/Multiple/ConnectionManagerConsecutive.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,28 @@ public function __construct(array $managers)
2323
$this->managers = $managers;
2424
}
2525

26-
public function create($host, $port)
26+
public function connect($uri)
2727
{
28-
return $this->tryConnection($this->managers, $host, $port);
28+
return $this->tryConnection($this->managers, $uri);
2929
}
3030

3131
/**
3232
*
3333
* @param ConnectorInterface[] $managers
34-
* @param string $host
35-
* @param int $port
34+
* @param string $uri
3635
* @return Promise
3736
* @internal
3837
*/
39-
public function tryConnection(array $managers, $host, $port)
38+
public function tryConnection(array $managers, $uri)
4039
{
41-
return new Promise\Promise(function ($resolve, $reject) use (&$managers, &$pending, $host, $port) {
42-
$try = function () use (&$try, &$managers, $host, $port, $resolve, $reject, &$pending) {
40+
return new Promise\Promise(function ($resolve, $reject) use (&$managers, &$pending, $uri) {
41+
$try = function () use (&$try, &$managers, $uri, $resolve, $reject, &$pending) {
4342
if (!$managers) {
4443
return $reject(new UnderflowException('No more managers to try to connect through'));
4544
}
4645

4746
$manager = array_shift($managers);
48-
$pending = $manager->create($host, $port);
47+
$pending = $manager->connect($uri);
4948
$pending->then($resolve, $try);
5049
};
5150

src/Multiple/ConnectionManagerRandom.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
class ConnectionManagerRandom extends ConnectionManagerConsecutive
66
{
7-
public function create($host, $port)
7+
public function connect($uri)
88
{
99
$managers = $this->managers;
1010
shuffle($managers);
11-
12-
return $this->tryConnection($managers, $host, $port);
11+
12+
return $this->tryConnection($managers, $uri);
1313
}
1414
}

0 commit comments

Comments
 (0)