From e2b7a8f95acc578ffa3ecb4aec8b00f75893a913 Mon Sep 17 00:00:00 2001 From: Illia Kovalov Date: Wed, 4 Oct 2017 12:03:25 +0200 Subject: [PATCH] * @param int $keepAliveInterval Seconds between ping calls --- src/Ratchet/App.php | 9 +++------ src/Ratchet/WebSocket/WsServer.php | 23 ++++++++++++++++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 5c7fbf0..f378534 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -97,24 +97,21 @@ class App { * @param ComponentInterface $controller Your application to server for the route. If not specified, assumed to be for a WebSocket * @param array $allowedOrigins An array of hosts allowed to connect (same host by default), ['*'] for any * @param string $httpHost Override the $httpHost variable provided in the __construct - * @param int $keepAliveInterval Seconds between ping calls. Works for WsServer only * @return ComponentInterface|WsServer */ - public function route($path, ComponentInterface $controller, array $allowedOrigins = array(), $httpHost = null, $keepAliveInterval = 30) { + public function route($path, ComponentInterface $controller, array $allowedOrigins = array(), $httpHost = null) { if ($controller instanceof HttpServerInterface || $controller instanceof WsServer) { $decorated = $controller; } elseif ($controller instanceof WampServerInterface) { $decorated = new WsServer(new WampServer($controller)); + $decorated->enableKeepAlive($this->_server->loop); } elseif ($controller instanceof MessageComponentInterface) { $decorated = new WsServer($controller); + $decorated->enableKeepAlive($this->_server->loop); } else { $decorated = $controller; } - if ($decorated instanceof WsServer) { - $decorated->enableKeepAlive($this->_server->loop, $keepAliveInterval); - } - if ($httpHost === null) { $httpHost = $this->httpHost; } diff --git a/src/Ratchet/WebSocket/WsServer.php b/src/Ratchet/WebSocket/WsServer.php index 8030604..951e9dd 100644 --- a/src/Ratchet/WebSocket/WsServer.php +++ b/src/Ratchet/WebSocket/WsServer.php @@ -61,11 +61,22 @@ class WsServer implements HttpServerInterface { */ 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 int $keepAliveTimeout * @note If you want to enable sub-protocols have your component implement WsServerInterface as well */ - public function __construct(ComponentInterface $component) { + public function __construct(ComponentInterface $component, $keepAliveTimeout = 30) { if ($component instanceof MessageComponentInterface) { $this->msgCb = function(ConnectionInterface $conn, MessageInterface $msg) { $this->delegate->onMessage($conn, $msg); @@ -88,6 +99,7 @@ class WsServer implements HttpServerInterface { $this->closeFrameChecker = new CloseFrameChecker; $this->handshakeNegotiator = new ServerNegotiator(new RequestVerifier); $this->handshakeNegotiator->setStrictSubProtocolCheck(true); + $this->keepAliveEnabled = false; if ($component instanceof WsServerInterface) { $this->handshakeNegotiator->setSupportedSubProtocols($component->getSubProtocols()); @@ -99,6 +111,7 @@ class WsServer implements HttpServerInterface { $this->ueFlowFactory = function() use ($reusableUnderflowException) { return $reusableUnderflowException; }; + $this->keepAliveTimeout = $keepAliveTimeout; } /** @@ -195,7 +208,11 @@ class WsServer implements HttpServerInterface { $this->handshakeNegotiator->setStrictSubProtocolCheck($enable); } - public function enableKeepAlive(LoopInterface $loop, $interval = 30) { + public function enableKeepAlive(LoopInterface $loop) { + if ($this->keepAliveEnabled) { + return; + } + $this->keepAliveEnabled = true; $lastPing = new Frame(uniqid(), true, Frame::OP_PING); $pingedConnections = new \SplObjectStorage; $splClearer = new \SplObjectStorage; @@ -206,7 +223,7 @@ class WsServer implements HttpServerInterface { } }; - $loop->addPeriodicTimer((int)$interval, function() use ($pingedConnections, &$lastPing, $splClearer) { + $loop->addPeriodicTimer((int)$this->keepAliveTimeout, function() use ($pingedConnections, &$lastPing, $splClearer) { foreach ($pingedConnections as $wsConn) { $wsConn->close(); }