def close(self):
        """Closes the WebSocket connection."""
        self._started = False

        if not self.server_terminated:
            if not self.stream.closed():
                self._write_frame(True, 0x8, b'')
            self.server_terminated = True

        if self.graceful_shutdown:
            if self.client_terminated:
                if self._waiting is not None:
                    try:
                        self.stream.io_loop.remove_timeout(self._waiting)
                    except __HOLE__:
                        pass
                    self._waiting = None
                self._terminate()
            elif self._waiting is None:
                # Give the client a few seconds to complete a clean shutdown,
                # otherwise just close the connection.
                self._waiting = self.stream.io_loop.add_timeout(
                    time.time() + self.graceful_shutdown, self._abort)
        else:
            if self.client_terminated:
                return

            self._terminate()
dataset/ETHPy150Open benoitc/gaffer/gaffer/httpclient/websocket.py/WebSocket.close