Skip to content

Commit 907cb34

Browse files
authored
Merge pull request #6 from WyriHaximus/update-to-react-http-1.0
Update to react/http 1.0
2 parents 6c1f81a + 35b83d8 commit 907cb34

5 files changed

Lines changed: 20 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $ws = new WebSocketMiddleware([], function (WebSocketConnection $conn) {
2222
});
2323
});
2424

25-
$server = new Server([$ws]);
25+
$server = new Server($loop, $ws);
2626

2727
$server->listen(new \React\Socket\Server('127.0.0.1:4321', $loop));
2828

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
],
2424
"require": {
2525
"ratchet/rfc6455": "^0.2.3",
26-
"react/http": "^0.8"
26+
"react/http": "^1.0"
2727
},
2828
"require-dev":{
2929
"react/child-process": "^0.5.0"

example/chat_ws_server.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Psr\Http\Message\ServerRequestInterface;
55
use Ratchet\RFC6455\Messaging\Message;
66
use React\EventLoop\Factory;
7-
use React\Http\Response;
7+
use React\Http\Message\Response;
88
use React\Http\Server;
99
use React\Stream\ThroughStream;
1010
use Voryx\WebSocketMiddleware\WebSocketConnection;
@@ -52,7 +52,8 @@
5252
$user++;
5353
});
5454

55-
$server = new Server([
55+
$server = new Server(
56+
$loop,
5657
function (ServerRequestInterface $request, callable $next) use ($broadcast) {
5758
// lets let the people chatting see what requests are happening too.
5859
$broadcast->write('<i>Request: ' . $request->getUri()->getPath() . '</i>');
@@ -66,9 +67,12 @@ function (ServerRequestInterface $request, callable $next) {
6667
function (ServerRequestInterface $request) use ($frontend) {
6768
return new Response(200, [], $frontend);
6869
},
69-
]);
70+
);
7071

7172
$server->listen(new \React\Socket\Server('127.0.0.1:4321', $loop));
73+
$server->on('error', static function (Throwable $e) {
74+
echo $e;
75+
});
7276

7377
openWebPage($loop, 'http://' . $uri);
7478

src/WebSocketMiddleware.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Psr\Http\Message\ServerRequestInterface;
66
use Ratchet\RFC6455\Handshake\RequestVerifier;
77
use Ratchet\RFC6455\Handshake\ServerNegotiator;
8-
use React\Http\Response;
8+
use React\Http\Message\Response;
99
use React\Stream\CompositeStream;
1010
use React\Stream\ThroughStream;
1111

@@ -22,11 +22,15 @@ public function __construct(array $paths = [], callable $connectionHandler = nul
2222
$this->subProtocols = $subProtocols;
2323
}
2424

25-
public function __invoke(ServerRequestInterface $request, callable $next)
25+
public function __invoke(ServerRequestInterface $request, callable $next = null)
2626
{
2727
// check path at some point - for now we just go go ws
2828
if (count($this->paths) > 0) {
2929
if (!in_array($request->getUri()->getPath(), $this->paths)) {
30+
if ($next === null) {
31+
return new Response(404);
32+
}
33+
3034
return $next($request);
3135
}
3236
}
@@ -38,6 +42,10 @@ public function __invoke(ServerRequestInterface $request, callable $next)
3842
$response = $negotiator->handshake($request);
3943

4044
if ($response->getStatusCode() != '101') {
45+
if ($next === null) {
46+
return new Response(404);
47+
}
48+
4149
// TODO: this should return an error or something not continue the chain
4250
return $next($request);
4351
}

tests/ab/testServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
});
1717
});
1818

19-
$server = new Server([$ws]);
19+
$server = new Server($loop, $ws);
2020

2121
$server->listen(new \React\Socket\Server('127.0.0.1:4321', $loop));
2222

0 commit comments

Comments
 (0)