Skip to content

Commit

Permalink
Merge pull request #6 from WyriHaximus/update-to-react-http-1.0
Browse files Browse the repository at this point in the history
Update to react/http 1.0
  • Loading branch information
mbonneau committed Aug 9, 2020
2 parents 6c1f81a + 35b83d8 commit 907cb34
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ws = new WebSocketMiddleware([], function (WebSocketConnection $conn) {
});
});

$server = new Server([$ws]);
$server = new Server($loop, $ws);

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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
"require": {
"ratchet/rfc6455": "^0.2.3",
"react/http": "^0.8"
"react/http": "^1.0"
},
"require-dev":{
"react/child-process": "^0.5.0"
Expand Down
10 changes: 7 additions & 3 deletions example/chat_ws_server.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Ratchet\RFC6455\Messaging\Message;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;
use React\Stream\ThroughStream;
use Voryx\WebSocketMiddleware\WebSocketConnection;
Expand Down Expand Up @@ -52,7 +52,8 @@
$user++;
});

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

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

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

Expand Down
12 changes: 10 additions & 2 deletions src/WebSocketMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Ratchet\RFC6455\Handshake\RequestVerifier;
use Ratchet\RFC6455\Handshake\ServerNegotiator;
use React\Http\Response;
use React\Http\Message\Response;
use React\Stream\CompositeStream;
use React\Stream\ThroughStream;

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

public function __invoke(ServerRequestInterface $request, callable $next)
public function __invoke(ServerRequestInterface $request, callable $next = null)
{
// check path at some point - for now we just go go ws
if (count($this->paths) > 0) {
if (!in_array($request->getUri()->getPath(), $this->paths)) {
if ($next === null) {
return new Response(404);
}

return $next($request);
}
}
Expand All @@ -38,6 +42,10 @@ public function __invoke(ServerRequestInterface $request, callable $next)
$response = $negotiator->handshake($request);

if ($response->getStatusCode() != '101') {
if ($next === null) {
return new Response(404);
}

// TODO: this should return an error or something not continue the chain
return $next($request);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ab/testServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
});
});

$server = new Server([$ws]);
$server = new Server($loop, $ws);

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

Expand Down

0 comments on commit 907cb34

Please sign in to comment.