Merge pull request #200 from cboden/closing

[WebSocket] Halt communication after closing frame
This commit is contained in:
Chris Boden 2014-06-08 11:17:27 -04:00
commit 0dfd2b83b6
3 changed files with 29 additions and 6 deletions

View File

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

View File

@ -8,12 +8,17 @@ use Ratchet\WebSocket\Version\DataInterface;
* @property \StdClass $WebSocket
*/
class Connection extends AbstractConnectionDecorator {
/**
* {@inheritdoc}
*/
public function send($msg) {
if (!$this->WebSocket->closing) {
if (!($msg instanceof DataInterface)) {
$msg = new Frame($msg);
}
$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;
}
}

View File

@ -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);
}