-
Notifications
You must be signed in to change notification settings - Fork 101
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
6 changed files
with
232 additions
and
16 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,27 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Http\Middleware; | ||
|
||
use Closure; | ||
use Laravel\Reverb\Server; | ||
use Laravel\Reverb\WebSockets\Request as WebSocketRequest; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class WebSocketMiddleware | ||
{ | ||
public function __construct(protected Server $server) | ||
{ | ||
|
||
} | ||
|
||
public function __invoke(ServerRequestInterface $request, Closure $next) | ||
{ | ||
$wsRequest = new WebSocketRequest($request); | ||
|
||
if (! $wsRequest->isWebSocketRequest()) { | ||
return $next($request); | ||
} | ||
|
||
return $wsRequest->negotiate($request); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\WebSockets; | ||
|
||
use Illuminate\Support\Facades\App; | ||
use Laravel\Reverb\Application; | ||
use Laravel\Reverb\Concerns\GeneratesPusherIdentifiers; | ||
use Laravel\Reverb\Connection as ReverbConnection; | ||
use Laravel\Reverb\Contracts\ConnectionManager; | ||
use Laravel\Reverb\Server; | ||
use Ratchet\RFC6455\Messaging\CloseFrameChecker; | ||
use Ratchet\RFC6455\Messaging\Message; | ||
use Ratchet\RFC6455\Messaging\MessageBuffer; | ||
|
||
class Connection extends ReverbConnection | ||
{ | ||
use GeneratesPusherIdentifiers; | ||
|
||
/** | ||
* The normalized socket ID. | ||
* | ||
* @var string | ||
*/ | ||
protected $id; | ||
|
||
protected $buffer; | ||
|
||
public function __construct(protected WsConnection $connection, Application $application, string $origin = null) | ||
{ | ||
parent::__construct($application, $origin); | ||
|
||
$this->buffer = new MessageBuffer( | ||
new CloseFrameChecker, | ||
onMessage: function (Message $message) { | ||
App::make(Server::class)->message($this, $message->getPayload()); | ||
}, | ||
sender: [$connection->stream, 'write'] | ||
); | ||
|
||
App::make(ConnectionManager::class)->for($application)->resolve( | ||
$connection->resourceId, | ||
fn () => $this | ||
); | ||
|
||
App::make(Server::class)->open($this); | ||
$connection->stream->on('data', [$this->buffer, 'onData']); | ||
$connection->stream->on('close', function () { | ||
App::make(Server::class)->close($this); | ||
}); | ||
} | ||
|
||
/** | ||
* Get the raw socket connection identifier. | ||
*/ | ||
public function identifier(): string | ||
{ | ||
return (string) $this->connection->resourceId; | ||
} | ||
|
||
/** | ||
* Get the normalized socket ID. | ||
*/ | ||
public function id(): string | ||
{ | ||
if (! $this->id) { | ||
$this->id = $this->generateId(); | ||
} | ||
|
||
return $this->id; | ||
} | ||
|
||
public static function make(WsConnection $connection, $application, $origin) | ||
{ | ||
return new static($connection, $application, $origin); | ||
} | ||
|
||
public function send(string $message): void | ||
{ | ||
$this->buffer->sendMessage($message); | ||
} | ||
|
||
public function terminate(): void | ||
{ | ||
$this->connection->stream->close(); | ||
} | ||
} |
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,76 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\WebSockets; | ||
|
||
use Illuminate\Support\Facades\App; | ||
use Laravel\Reverb\Contracts\ApplicationProvider; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Ratchet\RFC6455\Handshake\RequestVerifier; | ||
use Ratchet\RFC6455\Handshake\ServerNegotiator; | ||
use React\Http\Message\Response; | ||
use React\Stream\CompositeStream; | ||
use React\Stream\ThroughStream; | ||
|
||
class Request | ||
{ | ||
protected $connection; | ||
|
||
public function __construct(protected ServerRequestInterface $request) | ||
{ | ||
|
||
} | ||
|
||
public function isWebSocketRequest() | ||
{ | ||
$upgrade = $this->request->getHeader('Upgrade')[0] ?? null; | ||
|
||
return $upgrade === 'websocket'; | ||
} | ||
|
||
public function negotiate() | ||
{ | ||
$negotiator = new ServerNegotiator(new RequestVerifier); | ||
$response = $negotiator->handshake($this->request); | ||
|
||
if ($response->getStatusCode() != '101') { | ||
return false; | ||
} | ||
|
||
$inStream = new ThroughStream(); | ||
$outStream = new ThroughStream(); | ||
|
||
$this->connect($inStream, $outStream); | ||
|
||
return new Response( | ||
$response->getStatusCode(), | ||
$response->getHeaders(), | ||
new CompositeStream($outStream, $inStream) | ||
); | ||
} | ||
|
||
public function connect($inStream, $outStream) | ||
{ | ||
return $this->connection = Connection::make( | ||
new WsConnection(new CompositeStream($inStream, $outStream)), | ||
$this->application(), | ||
$this->origin(), | ||
); | ||
} | ||
|
||
public function connection() | ||
{ | ||
return $this->connection; | ||
} | ||
|
||
protected function application() | ||
{ | ||
parse_str($this->request->getUri()->getQuery(), $queryString); | ||
|
||
return App::make(ApplicationProvider::class)->findByKey($queryString['appId']); | ||
} | ||
|
||
protected function origin() | ||
{ | ||
return $this->request->getHeader('Origin')[0] ?? null; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\WebSockets; | ||
|
||
use React\Stream\CompositeStream; | ||
use Illuminate\Support\Str; | ||
use React\Stream\DuplexStreamInterface; | ||
|
||
class WsConnection | ||
{ | ||
public string $resourceId; | ||
|
||
public function __construct(public DuplexStreamInterface $stream) | ||
{ | ||
$this->resourceId = (string) Str::uuid(); | ||
} | ||
} |