diff --git a/src/Ratchet/WebSocket/Version/Hixie76/Connection.php b/src/Ratchet/WebSocket/Version/Hixie76/Connection.php index 2a5f845..e3d0834 100644 --- a/src/Ratchet/WebSocket/Version/Hixie76/Connection.php +++ b/src/Ratchet/WebSocket/Version/Hixie76/Connection.php @@ -8,12 +8,19 @@ use Ratchet\AbstractConnectionDecorator; */ class Connection extends AbstractConnectionDecorator { public function send($msg) { - $this->getConnection()->send(chr(0) . $msg . chr(255)); + if (!$this->WebSocket->closing) { + $this->getConnection()->send(chr(0) . $msg . chr(255)); + } return $this; } public function close() { - $this->getConnection()->close(); + if (!$this->WebSocket->closing) { + $this->getConnection()->send(chr(255)); + $this->getConnection()->close(); + + $this->WebSocket->closing = true; + } } } diff --git a/src/Ratchet/WebSocket/Version/RFC6455/Connection.php b/src/Ratchet/WebSocket/Version/RFC6455/Connection.php index 5bfa4a9..a17e382 100644 --- a/src/Ratchet/WebSocket/Version/RFC6455/Connection.php +++ b/src/Ratchet/WebSocket/Version/RFC6455/Connection.php @@ -8,12 +8,17 @@ use Ratchet\WebSocket\Version\DataInterface; * @property \StdClass $WebSocket */ class Connection extends AbstractConnectionDecorator { + /** + * {@inheritdoc} + */ public function send($msg) { - if (!($msg instanceof DataInterface)) { - $msg = new Frame($msg); - } + if (!$this->WebSocket->closing) { + if (!($msg instanceof DataInterface)) { + $msg = new Frame($msg); + } - $this->getConnection()->send($msg->getContents()); + $this->getConnection()->send($msg->getContents()); + } return $this; } @@ -22,6 +27,10 @@ class Connection extends AbstractConnectionDecorator { * {@inheritdoc} */ public function close($code = 1000) { + if ($this->WebSocket->closing) { + return; + } + if ($code instanceof DataInterface) { $this->send($code); } else { @@ -29,5 +38,7 @@ class Connection extends AbstractConnectionDecorator { } $this->getConnection()->close(); + + $this->WebSocket->closing = true; } } diff --git a/src/Ratchet/WebSocket/WsServer.php b/src/Ratchet/WebSocket/WsServer.php index 8ad6e4e..b4be1f0 100644 --- a/src/Ratchet/WebSocket/WsServer.php +++ b/src/Ratchet/WebSocket/WsServer.php @@ -79,6 +79,7 @@ class WsServer implements HttpServerInterface { $conn->WebSocket = new \StdClass; $conn->WebSocket->request = $request; $conn->WebSocket->established = false; + $conn->WebSocket->closing = false; $this->attemptUpgrade($conn); } @@ -87,6 +88,10 @@ class WsServer implements HttpServerInterface { * {@inheritdoc} */ public function onMessage(ConnectionInterface $from, $msg) { + if ($from->WebSocket->closing) { + return; + } + if (true === $from->WebSocket->established) { return $from->WebSocket->version->onMessage($this->connections[$from], $msg); }