Accept exception factory for performance gains

This commit is contained in:
Chris Boden 2015-12-22 21:11:46 -05:00
parent 3c3588fc8b
commit 1579666238
2 changed files with 9 additions and 5 deletions

View File

@ -53,15 +53,15 @@ class MessageStreamer {
CloseFrameChecker $frameChecker,
callable $onMessage,
callable $onControl = null,
$expectMask = true
$expectMask = true,
$exceptionFactory = null
) {
$this->validator = $encodingValidator;
$this->closeFrameChecker = $frameChecker;
$this->checkForMask = (bool)$expectMask;
$exception = new \UnderflowException;
$this->exceptionFactory = function() use ($exception) {
return $exception;
$this->exceptionFactory ?: $this->exceptionFactory = function($msg) {
return new \UnderflowException($msg);
};
$this->onMessage = $onMessage;

View File

@ -13,7 +13,9 @@ $encodingValidator = new \Ratchet\RFC6455\Encoding\Validator;
$closeFrameChecker = new \Ratchet\RFC6455\Messaging\Protocol\CloseFrameChecker;
$negotiator = new \Ratchet\RFC6455\Handshake\Negotiator($encodingValidator);
$server->on('request', function (\React\Http\Request $request, \React\Http\Response $response) use ($negotiator, $encodingValidator, $closeFrameChecker) {
$uException = new \UnderflowException;
$server->on('request', function (\React\Http\Request $request, \React\Http\Response $response) use ($negotiator, $encodingValidator, $closeFrameChecker, $uException) {
$psrRequest = new \GuzzleHttp\Psr7\Request($request->getMethod(), $request->getPath(), $request->getHeaders());
$negotiatorResponse = $negotiator->handshake($psrRequest);
@ -42,6 +44,8 @@ $server->on('request', function (\React\Http\Request $request, \React\Http\Respo
$response->write($parser->newFrame($frame->getPayload(), true, Frame::OP_PONG)->getContents());
break;
}
}, true, function() use ($uException) {
return $uException;
});
$request->on('data', [$parser, 'onData']);