Update WsServer.php

This commit is contained in:
Illia Kovalov 2017-10-09 17:50:50 +02:00 committed by GitHub
parent e2b7a8f95a
commit 8bbc516f5e

View File

@ -61,22 +61,12 @@ class WsServer implements HttpServerInterface {
*/ */
private $msgCb; private $msgCb;
/**
* @var bool
*/
private $keepAliveEnabled;
/**
* @var int
*/
private $keepAliveTimeout = 30;
/** /**
* @param \Ratchet\WebSocket\MessageComponentInterface|\Ratchet\MessageComponentInterface $component Your application to run with WebSockets * @param \Ratchet\WebSocket\MessageComponentInterface|\Ratchet\MessageComponentInterface $component Your application to run with WebSockets
* @param int $keepAliveTimeout * @param int $keepAliveTimeout
* @note If you want to enable sub-protocols have your component implement WsServerInterface as well * @note If you want to enable sub-protocols have your component implement WsServerInterface as well
*/ */
public function __construct(ComponentInterface $component, $keepAliveTimeout = 30) { public function __construct(ComponentInterface $component) {
if ($component instanceof MessageComponentInterface) { if ($component instanceof MessageComponentInterface) {
$this->msgCb = function(ConnectionInterface $conn, MessageInterface $msg) { $this->msgCb = function(ConnectionInterface $conn, MessageInterface $msg) {
$this->delegate->onMessage($conn, $msg); $this->delegate->onMessage($conn, $msg);
@ -99,7 +89,6 @@ class WsServer implements HttpServerInterface {
$this->closeFrameChecker = new CloseFrameChecker; $this->closeFrameChecker = new CloseFrameChecker;
$this->handshakeNegotiator = new ServerNegotiator(new RequestVerifier); $this->handshakeNegotiator = new ServerNegotiator(new RequestVerifier);
$this->handshakeNegotiator->setStrictSubProtocolCheck(true); $this->handshakeNegotiator->setStrictSubProtocolCheck(true);
$this->keepAliveEnabled = false;
if ($component instanceof WsServerInterface) { if ($component instanceof WsServerInterface) {
$this->handshakeNegotiator->setSupportedSubProtocols($component->getSubProtocols()); $this->handshakeNegotiator->setSupportedSubProtocols($component->getSubProtocols());
@ -208,10 +197,7 @@ class WsServer implements HttpServerInterface {
$this->handshakeNegotiator->setStrictSubProtocolCheck($enable); $this->handshakeNegotiator->setStrictSubProtocolCheck($enable);
} }
public function enableKeepAlive(LoopInterface $loop) { public function enableKeepAlive(LoopInterface $loop, $interval = 30) {
if ($this->keepAliveEnabled) {
return;
}
$this->keepAliveEnabled = true; $this->keepAliveEnabled = true;
$lastPing = new Frame(uniqid(), true, Frame::OP_PING); $lastPing = new Frame(uniqid(), true, Frame::OP_PING);
$pingedConnections = new \SplObjectStorage; $pingedConnections = new \SplObjectStorage;
@ -223,7 +209,7 @@ class WsServer implements HttpServerInterface {
} }
}; };
$loop->addPeriodicTimer((int)$this->keepAliveTimeout, function() use ($pingedConnections, &$lastPing, $splClearer) { $loop->addPeriodicTimer((int)$interval, function() use ($pingedConnections, &$lastPing, $splClearer) {
foreach ($pingedConnections as $wsConn) { foreach ($pingedConnections as $wsConn) {
$wsConn->close(); $wsConn->close();
} }