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, CloseFrameChecker $frameChecker,
callable $onMessage, callable $onMessage,
callable $onControl = null, callable $onControl = null,
$expectMask = true $expectMask = true,
$exceptionFactory = null
) { ) {
$this->validator = $encodingValidator; $this->validator = $encodingValidator;
$this->closeFrameChecker = $frameChecker; $this->closeFrameChecker = $frameChecker;
$this->checkForMask = (bool)$expectMask; $this->checkForMask = (bool)$expectMask;
$exception = new \UnderflowException; $this->exceptionFactory ?: $this->exceptionFactory = function($msg) {
$this->exceptionFactory = function() use ($exception) { return new \UnderflowException($msg);
return $exception;
}; };
$this->onMessage = $onMessage; $this->onMessage = $onMessage;

View File

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