Skip to content

Commit d44df87

Browse files
authored
Merge pull request #33 from PNixx/master
fix heartbeat interval
2 parents 0f86217 + 80f8c9f commit d44df87

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/Client.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function () {
9191

9292
$this->state = self::STATE_CONNECTING;
9393

94-
$this->connection = new Connection($this->config->uri());
94+
$this->connection = new Connection($this->config->uri(), fn() => $this->state = self::STATE_NOT_CONNECTED);
9595

9696
yield $this->connection->open(
9797
$this->config->timeout,
@@ -287,7 +287,7 @@ function () {
287287
$heartbeatInterval = $this->config->heartbeat;
288288

289289
if ($heartbeatInterval !== 0) {
290-
$heartbeatInterval = \min($heartbeatInterval, $tune->heartbeat);
290+
$heartbeatInterval = \min($heartbeatInterval, $tune->heartbeat * 1000);
291291
}
292292

293293
$maxChannel = \min($this->config->maxChannel, $tune->channelMax);
@@ -302,17 +302,15 @@ function () {
302302
->appendUint16(31)
303303
->appendInt16($maxChannel)
304304
->appendInt32($maxFrame)
305-
->appendInt16($heartbeatInterval)
305+
->appendInt16((int) ($heartbeatInterval / 1000))
306306
->appendUint8(206);
307307

308308
yield $this->connection->write($buffer);
309309

310310
$this->properties->tune($maxChannel, $maxFrame);
311311

312312
if ($heartbeatInterval > 0) {
313-
$this->connection->heartbeat($heartbeatInterval, function (){
314-
$this->state = self::STATE_NOT_CONNECTED;
315-
});
313+
$this->connection->heartbeat($heartbeatInterval);
316314
}
317315
}
318316
);

src/Connection.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ final class Connection
5858
*/
5959
private $heartbeatWatcherId;
6060

61-
public function __construct(string $uri)
61+
/**
62+
* @var callable|null
63+
*/
64+
private $connectionLost;
65+
66+
public function __construct(string $uri, ?callable $connectionLost = null)
6267
{
6368
$this->uri = $uri;
6469
$this->parser = new Parser;
70+
$this->connectionLost = $connectionLost;
6571
}
6672

6773
/**
@@ -164,11 +170,11 @@ function () {
164170
);
165171
}
166172

167-
public function heartbeat(int $interval, ?callable $connectionLost = null): void
173+
public function heartbeat(int $interval): void
168174
{
169175
$this->heartbeatWatcherId = Loop::repeat(
170176
$interval,
171-
function (string $watcherId) use ($interval, $connectionLost){
177+
function (string $watcherId) use ($interval){
172178
$currentTime = Loop::now();
173179

174180
if (null !== $this->socket) {
@@ -189,12 +195,12 @@ function (string $watcherId) use ($interval, $connectionLost){
189195
}
190196

191197
if (
192-
null !== $connectionLost &&
198+
null !== $this->connectionLost &&
193199
0 !== $this->lastRead &&
194200
$currentTime > ($this->lastRead + $interval + 1000)
195201
)
196202
{
197-
$connectionLost();
203+
call_user_func($this->connectionLost);
198204
Loop::cancel($watcherId);
199205
}
200206

@@ -212,6 +218,10 @@ public function close(): void
212218
$this->heartbeatWatcherId = null;
213219
}
214220

221+
if ($this->connectionLost !== null) {
222+
call_user_func($this->connectionLost);
223+
}
224+
215225
if ($this->socket !== null) {
216226
$this->socket->close();
217227
}

0 commit comments

Comments
 (0)