[WebSocket] Halt communication after closing frame

Also send closing frame in Hixie
This commit is contained in:
Chris Boden 2014-06-08 10:55:57 -04:00
parent 33c59a8de7
commit 80124ec05e
3 changed files with 29 additions and 6 deletions

View File

@ -8,12 +8,19 @@ use Ratchet\AbstractConnectionDecorator;
*/ */
class Connection extends AbstractConnectionDecorator { class Connection extends AbstractConnectionDecorator {
public function send($msg) { public function send($msg) {
if (!$this->WebSocket->closing) {
$this->getConnection()->send(chr(0) . $msg . chr(255)); $this->getConnection()->send(chr(0) . $msg . chr(255));
}
return $this; return $this;
} }
public function close() { public function close() {
if (!$this->WebSocket->closing) {
$this->getConnection()->send(chr(255));
$this->getConnection()->close(); $this->getConnection()->close();
$this->WebSocket->closing = true;
}
} }
} }

View File

@ -8,12 +8,17 @@ use Ratchet\WebSocket\Version\DataInterface;
* @property \StdClass $WebSocket * @property \StdClass $WebSocket
*/ */
class Connection extends AbstractConnectionDecorator { class Connection extends AbstractConnectionDecorator {
/**
* {@inheritdoc}
*/
public function send($msg) { public function send($msg) {
if (!$this->WebSocket->closing) {
if (!($msg instanceof DataInterface)) { if (!($msg instanceof DataInterface)) {
$msg = new Frame($msg); $msg = new Frame($msg);
} }
$this->getConnection()->send($msg->getContents()); $this->getConnection()->send($msg->getContents());
}
return $this; return $this;
} }
@ -22,6 +27,10 @@ class Connection extends AbstractConnectionDecorator {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function close($code = 1000) { public function close($code = 1000) {
if ($this->WebSocket->closing) {
return;
}
if ($code instanceof DataInterface) { if ($code instanceof DataInterface) {
$this->send($code); $this->send($code);
} else { } else {
@ -29,5 +38,7 @@ class Connection extends AbstractConnectionDecorator {
} }
$this->getConnection()->close(); $this->getConnection()->close();
$this->WebSocket->closing = true;
} }
} }

View File

@ -79,6 +79,7 @@ class WsServer implements HttpServerInterface {
$conn->WebSocket = new \StdClass; $conn->WebSocket = new \StdClass;
$conn->WebSocket->request = $request; $conn->WebSocket->request = $request;
$conn->WebSocket->established = false; $conn->WebSocket->established = false;
$conn->WebSocket->closing = false;
$this->attemptUpgrade($conn); $this->attemptUpgrade($conn);
} }
@ -87,6 +88,10 @@ class WsServer implements HttpServerInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function onMessage(ConnectionInterface $from, $msg) { public function onMessage(ConnectionInterface $from, $msg) {
if ($from->WebSocket->closing) {
return;
}
if (true === $from->WebSocket->established) { if (true === $from->WebSocket->established) {
return $from->WebSocket->version->onMessage($this->connections[$from], $msg); return $from->WebSocket->version->onMessage($this->connections[$from], $msg);
} }