Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Commit

Permalink
Connection: Add close-before event.
Browse files Browse the repository at this point in the history
  • Loading branch information
Metalaka committed Mar 30, 2017
1 parent 834b4ab commit bead328
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
29 changes: 16 additions & 13 deletions Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public function __construct(HoaSocket\Connection $connection)
'message',
'binary-message',
'ping',
'close-before',
'close',
'error'
]
Expand Down Expand Up @@ -464,29 +465,23 @@ protected function _run(HoaSocket\Node $node)
}

try {
$this->close(self::CLOSE_NORMAL);
} catch (HoaException\Idle $e) {
// Cannot properly close the connection because the
// client might already be disconnected.
} finally {
$this->getListener()->fire(
'close',
new Event\Bucket([
'code' => $code,
'reason' => $reason
])
);
$this->close(self::CLOSE_NORMAL);
} catch (HoaException\Idle $e) {
// Cannot properly close the connection because the
// client might already be disconnected.
}

break;

default:
$this->getListener()->fire(
'close',
new Event\Bucket([
'code' =>self::CLOSE_PROTOCOL_ERROR,
'reason' => null
])
);
$this->close(self::CLOSE_PROTOCOL_ERROR);
}
} catch (HoaException\Idle $e) {
Expand Down Expand Up @@ -586,13 +581,21 @@ public function close($code = self::CLOSE_NORMAL, $reason = null)
$protocol = $connection->getCurrentNode()->getProtocolImplementation();

try {
$this->getListener()->fire(
'close-before',
new Event\Bucket([
'code' => $code,
'reason' => $reason
])
);

if (null !== $protocol) {
$protocol->close($code, $reason);
}
} finally {
$out = $connection->disconnect();
$connection->disconnect();
}

return $out;
return;
}
}
2 changes: 2 additions & 0 deletions Test/Unit/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public function case_constructor()
->isTrue()
->boolean($listener->listenerExists('ping'))
->isTrue()
->boolean($listener->listenerExists('close-before'))
->isTrue()
->boolean($listener->listenerExists('close'))
->isTrue()
->boolean($listener->listenerExists('error'))
Expand Down

0 comments on commit bead328

Please sign in to comment.