-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
237 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb; | ||
|
||
use BadMethodCallException; | ||
use React\Socket\ConnectionInterface; | ||
|
||
class Conn | ||
{ | ||
protected $id; | ||
|
||
protected $initilized = false; | ||
|
||
protected $buffer = ''; | ||
|
||
public function __construct(protected ConnectionInterface $connection) | ||
{ | ||
$this->id = (int) $connection->stream; | ||
} | ||
|
||
public function id() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function initialize() | ||
{ | ||
$this->initilized = true; | ||
} | ||
|
||
public function isInitialized() | ||
{ | ||
return $this->initilized; | ||
} | ||
|
||
public function hasBuffer() | ||
{ | ||
return $this->buffer !== ''; | ||
} | ||
|
||
public function send($data) | ||
{ | ||
$this->connection->write($data); | ||
|
||
return $this; | ||
} | ||
|
||
public function close() | ||
{ | ||
$this->connection->end(); | ||
|
||
return $this; | ||
} | ||
|
||
public function __call($method, $parameters) | ||
{ | ||
if (! method_exists($this->connection, $method)) { | ||
throw new BadMethodCallException("Method [{$method}] does not exist on [".get_class($this->connection).'].'); | ||
} | ||
|
||
return $this->connection->{$method}(...$parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb; | ||
|
||
use GuzzleHttp\Psr7\Message; | ||
use GuzzleHttp\Psr7\Response; | ||
use Laravel\Reverb\Contracts\ApplicationProvider; | ||
use Laravel\Reverb\Contracts\ConnectionManager; | ||
use Laravel\Reverb\Servers\Reverb\Connection; | ||
use Laravel\Reverb\WebSockets\Request as WebSocketRequest; | ||
use Laravel\Reverb\WebSockets\WsConnection; | ||
use Psr\Http\Message\RequestInterface; | ||
use Ratchet\RFC6455\Handshake\RequestVerifier; | ||
use Ratchet\RFC6455\Handshake\ServerNegotiator; | ||
use React\EventLoop\Loop; | ||
use React\EventLoop\LoopInterface; | ||
use React\Socket\ConnectionInterface; | ||
use React\Socket\ServerInterface; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
|
||
class HttpServer | ||
{ | ||
protected $loop; | ||
|
||
public function __construct(protected ServerInterface $socket, LoopInterface $loop = null) | ||
{ | ||
$this->loop = $this->loop ?: Loop::get(); | ||
|
||
$socket->on('connection', $this); | ||
} | ||
|
||
public function __invoke(ConnectionInterface $connection) | ||
{ | ||
$connection = new Conn($connection); | ||
|
||
$connection->on('data', function ($data) use ($connection) { | ||
$this->handleRequest($data, $connection); | ||
}); | ||
// $conn->on('close', function () use ($conn) { | ||
// $this->handleEnd($conn); | ||
// }); | ||
// $conn->on('error', function (\Exception $e) use ($conn) { | ||
// $this->handleError($e, $conn); | ||
// }); | ||
} | ||
|
||
public function start() | ||
{ | ||
$this->loop->run(); | ||
} | ||
|
||
protected function handleRequest(string $data, Conn $connection) | ||
{ | ||
if(! $connection->isInitialized()) { | ||
$request = Request::from($data); | ||
$connection->initialize(); | ||
|
||
if($request->getUri()->getPath() === '/app/yysmuc8zbg4vo2hxgk9w') { | ||
$negotiator = new ServerNegotiator(new RequestVerifier); | ||
$response = $negotiator->handshake($request); | ||
|
||
$connection = new WsConnection($connection); | ||
|
||
$connection->send(Message::toString($response)); | ||
|
||
$server = app(Server::class); | ||
$reverbConnection = $this->connection($request, $connection); | ||
$server->open($reverbConnection); | ||
|
||
$connection->on('message', fn (string $message) => $server->message($reverbConnection, $message)); | ||
|
||
return; | ||
} | ||
|
||
$payload = json_decode($request->getBody()->getContents(), true); | ||
|
||
Event::dispatch($this->application($request), [ | ||
'event' => $payload['name'], | ||
'channel' => $payload['channel'], | ||
'data' => $payload['data'], | ||
]); | ||
|
||
return tap($connection)->send(new JsonResponse((object) []))->close(); | ||
} | ||
} | ||
|
||
protected function connection(RequestInterface $request, WsConnection $connection): Connection | ||
{ | ||
return app(ConnectionManager::class) | ||
->for($application = $this->application($request)) | ||
->resolve( | ||
$connection->id(), | ||
fn () => new Connection( | ||
$connection, | ||
$application, | ||
$request->getHeader('Origin')[0] ?? null | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Get the application instance for the request. | ||
*/ | ||
protected function application(RequestInterface $request): Application | ||
{ | ||
// parse_str($request->getUri()->getQuery(), $queryString); | ||
|
||
return app(ApplicationProvider::class)->findById('123456'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb; | ||
|
||
use GuzzleHttp\Psr7\Message; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class Request | ||
{ | ||
public static function from(string $message): RequestInterface | ||
{ | ||
return Message::parseRequest($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters