From 4aa084c3791615680bfa726c4a722f37f5bf5491 Mon Sep 17 00:00:00 2001 From: Illia Kovalov <ikovalyov85@gmail.com> Date: Thu, 28 Sep 2017 20:06:14 +0200 Subject: [PATCH 1/7] * @param int $keepAliveInterval Seconds between ping calls --- src/Ratchet/App.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index c119d41..e3bc5ab 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -97,9 +97,10 @@ 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 * @return ComponentInterface|WsServer */ - public function route($path, ComponentInterface $controller, array $allowedOrigins = array(), $httpHost = null) { + public function route($path, ComponentInterface $controller, array $allowedOrigins = array(), $httpHost = null, $keepAliveInterval = 30) { if ($controller instanceof HttpServerInterface || $controller instanceof WsServer) { $decorated = $controller; } elseif ($controller instanceof WampServerInterface) { @@ -111,7 +112,7 @@ class App { } if ($decorated instanceof WsServer) { - $decorated->enableKeepAlive($this->_server->loop, 30); + $decorated->enableKeepAlive($this->_server->loop, $keepAliveInterval); } if ($httpHost === null) { From f126b6836cf156c78ca30e36c856ff788cd259f8 Mon Sep 17 00:00:00 2001 From: Illia Kovalov <ikovalyov85@gmail.com> Date: Thu, 28 Sep 2017 20:07:33 +0200 Subject: [PATCH 2/7] * @param int $keepAliveInterval Seconds between ping calls --- src/Ratchet/App.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index e3bc5ab..c119d41 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -97,10 +97,9 @@ 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 * @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) { @@ -112,7 +111,7 @@ class App { } if ($decorated instanceof WsServer) { - $decorated->enableKeepAlive($this->_server->loop, $keepAliveInterval); + $decorated->enableKeepAlive($this->_server->loop, 30); } if ($httpHost === null) { From e0ee60e86005617214041eeeaec8784401a52953 Mon Sep 17 00:00:00 2001 From: Illia Kovalov <ikovalyov85@gmail.com> Date: Thu, 28 Sep 2017 20:08:09 +0200 Subject: [PATCH 3/7] * @param int $keepAliveInterval Seconds between ping calls --- src/Ratchet/App.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index c119d41..e3bc5ab 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -97,9 +97,10 @@ 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 * @return ComponentInterface|WsServer */ - public function route($path, ComponentInterface $controller, array $allowedOrigins = array(), $httpHost = null) { + public function route($path, ComponentInterface $controller, array $allowedOrigins = array(), $httpHost = null, $keepAliveInterval = 30) { if ($controller instanceof HttpServerInterface || $controller instanceof WsServer) { $decorated = $controller; } elseif ($controller instanceof WampServerInterface) { @@ -111,7 +112,7 @@ class App { } if ($decorated instanceof WsServer) { - $decorated->enableKeepAlive($this->_server->loop, 30); + $decorated->enableKeepAlive($this->_server->loop, $keepAliveInterval); } if ($httpHost === null) { From 467c4552a5d0ed38c6e48d621d38401e0f504e4b Mon Sep 17 00:00:00 2001 From: Illia Kovalov <ikovalyov85@gmail.com> Date: Thu, 28 Sep 2017 20:08:58 +0200 Subject: [PATCH 4/7] * @param int $keepAliveInterval Seconds between ping calls --- src/Ratchet/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index e3bc5ab..5c7fbf0 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -97,7 +97,7 @@ 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 + * @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) { From e2b7a8f95acc578ffa3ecb4aec8b00f75893a913 Mon Sep 17 00:00:00 2001 From: Illia Kovalov <ikovalyov85@gmail.com> Date: Wed, 4 Oct 2017 12:03:25 +0200 Subject: [PATCH 5/7] * @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(); } From 8bbc516f5e595496f327de41f2666e859d4126b2 Mon Sep 17 00:00:00 2001 From: Illia Kovalov <ikovalyov85@gmail.com> Date: Mon, 9 Oct 2017 17:50:50 +0200 Subject: [PATCH 6/7] Update WsServer.php --- src/Ratchet/WebSocket/WsServer.php | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/Ratchet/WebSocket/WsServer.php b/src/Ratchet/WebSocket/WsServer.php index 951e9dd..551c737 100644 --- a/src/Ratchet/WebSocket/WsServer.php +++ b/src/Ratchet/WebSocket/WsServer.php @@ -61,22 +61,12 @@ 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, $keepAliveTimeout = 30) { + public function __construct(ComponentInterface $component) { if ($component instanceof MessageComponentInterface) { $this->msgCb = function(ConnectionInterface $conn, MessageInterface $msg) { $this->delegate->onMessage($conn, $msg); @@ -99,7 +89,6 @@ 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()); @@ -208,10 +197,7 @@ class WsServer implements HttpServerInterface { $this->handshakeNegotiator->setStrictSubProtocolCheck($enable); } - public function enableKeepAlive(LoopInterface $loop) { - if ($this->keepAliveEnabled) { - return; - } + public function enableKeepAlive(LoopInterface $loop, $interval = 30) { $this->keepAliveEnabled = true; $lastPing = new Frame(uniqid(), true, Frame::OP_PING); $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) { $wsConn->close(); } From 4e8fca90327c5f63287351e01b46d44ebd0952eb Mon Sep 17 00:00:00 2001 From: Illia Kovalov <ikovalyov85@gmail.com> Date: Mon, 9 Oct 2017 17:51:28 +0200 Subject: [PATCH 7/7] Update WsServer.php --- src/Ratchet/WebSocket/WsServer.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Ratchet/WebSocket/WsServer.php b/src/Ratchet/WebSocket/WsServer.php index 551c737..8030604 100644 --- a/src/Ratchet/WebSocket/WsServer.php +++ b/src/Ratchet/WebSocket/WsServer.php @@ -63,7 +63,6 @@ class WsServer implements HttpServerInterface { /** * @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) { @@ -100,7 +99,6 @@ class WsServer implements HttpServerInterface { $this->ueFlowFactory = function() use ($reusableUnderflowException) { return $reusableUnderflowException; }; - $this->keepAliveTimeout = $keepAliveTimeout; } /** @@ -198,7 +196,6 @@ class WsServer implements HttpServerInterface { } public function enableKeepAlive(LoopInterface $loop, $interval = 30) { - $this->keepAliveEnabled = true; $lastPing = new Frame(uniqid(), true, Frame::OP_PING); $pingedConnections = new \SplObjectStorage; $splClearer = new \SplObjectStorage;