@@ -54,7 +54,7 @@ final class Connection
54
54
private $ lastRead = 0 ;
55
55
56
56
/**
57
- * @var string
57
+ * @var string|null
58
58
*/
59
59
private $ heartbeatWatcherId ;
60
60
@@ -168,35 +168,38 @@ public function heartbeat(int $interval, ?callable $connectionLost = null): void
168
168
{
169
169
$ this ->heartbeatWatcherId = Loop::repeat (
170
170
$ interval ,
171
- function ($ watcherId ) use ($ interval , $ connectionLost ){
172
- $ currentTime = Loop::now ();
171
+ function (string $ watcherId ) use ($ interval , $ connectionLost ){
172
+ $ currentTime = Loop::now ();
173
173
174
- if (null !== $ this ->socket ) {
175
- $ lastWrite = $ this ->lastWrite ?: $ currentTime ;
174
+ if (null !== $ this ->socket ) {
175
+ $ lastWrite = $ this ->lastWrite ?: $ currentTime ;
176
176
177
- $ nextHeartbeat = $ lastWrite + $ interval ;
177
+ $ nextHeartbeat = $ lastWrite + $ interval ;
178
178
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
+ }
187
187
188
- unset($ lastWrite , $ nextHeartbeat );
189
- }
188
+ unset($ lastWrite , $ nextHeartbeat );
189
+ }
190
190
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
+ {
193
197
$ connectionLost ();
194
198
Loop::cancel ($ watcherId );
195
199
}
196
- }
197
200
198
- unset($ currentTime );
199
- });
201
+ unset($ currentTime );
202
+ });
200
203
}
201
204
202
205
public function close (): void
0 commit comments