diff --git a/src/WebSocketConnection.php b/src/WebSocketConnection.php index 4490680..ad0bf32 100644 --- a/src/WebSocketConnection.php +++ b/src/WebSocketConnection.php @@ -181,6 +181,11 @@ class WebSocketConnection implements WebSocketInterface return $this->send(self::OP_FRAME_TEXT, $data); } + public function writeBinary($data) + { + return $this->send(self::OP_FRAME_BINARY, $data); + } + /** * */ diff --git a/src/WebSocketMiddleware.php b/src/WebSocketMiddleware.php index a5da28c..e075edd 100644 --- a/src/WebSocketMiddleware.php +++ b/src/WebSocketMiddleware.php @@ -37,6 +37,7 @@ class WebSocketMiddleware implements EventEmitterInterface public function __invoke(ServerRequestInterface $request, callable $next) { + if ($request->getHeaderLine('upgrade') !== 'websocket') { return $next($request); } @@ -56,9 +57,10 @@ class WebSocketMiddleware implements EventEmitterInterface $this->websockets->detach($websocket); }); - Loop::futureTick(function () use ($websocket) { + // FIXME how to test with futureTick? + //Loop::futureTick(function () use ($websocket) { $this->emit(self::EVENT_CONNECTION, [ $websocket ]); - }); + //}); return new Response( Response::STATUS_SWITCHING_PROTOCOLS, diff --git a/tests/WebSocketMiddlewareTest.php b/tests/WebSocketMiddlewareTest.php new file mode 100644 index 0000000..51b024f --- /dev/null +++ b/tests/WebSocketMiddlewareTest.php @@ -0,0 +1,57 @@ +getMockBuilder(InvokableDummy::class)->getMock(); + $mock->expects($this->never()) + ->method('test'); + + $next = $this->getMockBuilder(InvokableDummy::class)->getMock(); + $next->expects($this->once()) + ->method('test'); + + $request = new ServerRequest("GET", "/", [], ""); + + $middleware = new WebSocketMiddleware(); + $middleware->on('connection', [$mock,"test"]); + $middleware->__invoke($request, [$next,"test"]); + } + + public function testThatWebSocketRequestsAreHandled() + { + + $mock = $this->getMockBuilder(InvokableDummy::class)->getMock(); + $mock->expects($this->once()) + ->method('test'); + + $next = $this->getMockBuilder(InvokableDummy::class)->getMock(); + $next->expects($this->never()) + ->method('test'); + + $request = new ServerRequest("GET", "/", [ + 'upgrade' => [ 'websocket' ] + ]); + + $middleware = new WebSocketMiddleware(); + $middleware->on('connection', [$mock,"test"]); + $middleware->__invoke($request, [$next,"test"]); + } + +} + +class InvokableDummy +{ + public function test() + { + } +} \ No newline at end of file