@@ -54,7 +54,7 @@ final class Connection
5454 private $ lastRead = 0 ;
5555
5656 /**
57- * @var string
57+ * @var string|null
5858 */
5959 private $ heartbeatWatcherId ;
6060
@@ -168,35 +168,38 @@ public function heartbeat(int $interval, ?callable $connectionLost = null): void
168168 {
169169 $ this ->heartbeatWatcherId = Loop::repeat (
170170 $ interval ,
171- function ($ watcherId ) use ($ interval , $ connectionLost ){
172- $ currentTime = Loop::now ();
171+ function (string $ watcherId ) use ($ interval , $ connectionLost ){
172+ $ currentTime = Loop::now ();
173173
174- if (null !== $ this ->socket ) {
175- $ lastWrite = $ this ->lastWrite ?: $ currentTime ;
174+ if (null !== $ this ->socket ) {
175+ $ lastWrite = $ this ->lastWrite ?: $ currentTime ;
176176
177- $ nextHeartbeat = $ lastWrite + $ interval ;
177+ $ nextHeartbeat = $ lastWrite + $ interval ;
178178
179- if ($ currentTime >= $ nextHeartbeat ) {
180- yield $ this ->write ((new Buffer )
181- ->appendUint8 (8 )
182- ->appendUint16 (0 )
183- ->appendUint32 (0 )
184- ->appendUint8 (206 )
185- );
186- }
179+ if ($ currentTime >= $ nextHeartbeat ) {
180+ yield $ this ->write ((new Buffer )
181+ ->appendUint8 (8 )
182+ ->appendUint16 (0 )
183+ ->appendUint32 (0 )
184+ ->appendUint8 (206 )
185+ );
186+ }
187187
188- unset($ lastWrite , $ nextHeartbeat );
189- }
188+ unset($ lastWrite , $ nextHeartbeat );
189+ }
190190
191- if (null !== $ connectionLost && 0 !== $ this ->lastRead ) {
192- if ($ currentTime > ($ this ->lastRead + $ interval + 1000 )) {
191+ if (
192+ null !== $ connectionLost &&
193+ 0 !== $ this ->lastRead &&
194+ $ currentTime > ($ this ->lastRead + $ interval + 1000 )
195+ )
196+ {
193197 $ connectionLost ();
194198 Loop::cancel ($ watcherId );
195199 }
196- }
197200
198- unset($ currentTime );
199- });
201+ unset($ currentTime );
202+ });
200203 }
201204
202205 public function close (): void
0 commit comments