Merge pull request #200 from cboden/closing
[WebSocket] Halt communication after closing frame
This commit is contained in:
		
						commit
						0dfd2b83b6
					
				@ -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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user