Skip to content

Commit c026b64

Browse files
committed
Send hybi pings each 5 seconds
1 parent 41efe5d commit c026b64

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/Devristo/Phpws/Server/WebSocketServer.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Devristo\Phpws\Server;
44

5+
use Devristo\Phpws\Framing\WebSocketFrame;
6+
use Devristo\Phpws\Framing\WebSocketOpcode;
57
use Devristo\Phpws\Protocol\Handshake;
8+
use Devristo\Phpws\Protocol\WebSocketTransportHybi;
69
use Devristo\Phpws\Protocol\WebSocketTransportInterface;
710
use Devristo\Phpws\Protocol\WebSocketConnection;
811
use Evenement\EventEmitter;
@@ -151,8 +154,9 @@ public function bind()
151154
}
152155
});
153156

154-
$client->on("close", function () use ($that, $client, $logger) {
157+
$client->on("close", function () use ($that, $client, $logger, &$sockets, $client) {
155158
try{
159+
$sockets->detach($client);
156160
$connection = $client->getTransport();
157161

158162
if($connection){
@@ -171,8 +175,14 @@ public function bind()
171175
});
172176

173177
$this->loop->addPeriodicTimer(5, function () use ($timeOut, $sockets, $that) {
174-
$currentTime = time();
175178

179+
# Lets send some pings
180+
foreach($that->getConnections() as $c){
181+
if($c instanceof WebSocketTransportHybi)
182+
$c->sendFrame(WebSocketFrame::create(WebSocketOpcode::PingFrame));
183+
}
184+
185+
$currentTime = time();
176186
if ($timeOut == null)
177187
return;
178188

0 commit comments

Comments
 (0)