public function close()
{
if ($this->isClosing) {
return;
}
$this->isClosing = true;
$this->sendFrame(WebSocketFrame::create(WebSocketOpcode::CloseFrame));
$this->state = self::STATE_CLOSING;
$stream = $this->stream;
$closeTimer = $this->loop->addTimer(5, function () use($stream) {
$stream->close();
});
$loop = $this->loop;
$stream->once("close", function () use($closeTimer, $loop) {
if ($closeTimer) {
$loop->cancelTimer($closeTimer);
}
});
}