Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Dec 29, 2023
1 parent 7f69614 commit 3c288ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
28 changes: 19 additions & 9 deletions src/Servers/ApiGateway/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class Connection implements WebSocketConnection
{
protected bool $isFresh = true;

/**
* Create a new connection instance.
*/
Expand All @@ -29,15 +31,15 @@ public function id(): int|string
*/
public function send(mixed $message): void
{
info('Sending message to connection', [
'connection' => $this->identifier,
'message' => $message,
]);

app()->terminating(fn () => SendToConnection::dispatch(
$this->identifier,
$message
));
if ($this->isFresh()) {
SendToConnection::dispatch($this->identifier, $message);

$this->isFresh = false;

return;
}

SendToConnection::dispatchSync($this->identifier, $message);
}

/**
Expand All @@ -47,4 +49,12 @@ public function close(mixed $message = null): void
{
app(ConnectionManager::class)->forgetById($this->identifier);
}

/**
* Determine if the connection was instantiated during the current request.
*/
protected function isFresh(): bool
{
return $this->isFresh;
}
}
8 changes: 2 additions & 6 deletions src/Servers/ApiGateway/Jobs/SendToConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Laravel\Reverb\Servers\ApiGateway\Jobs;

use Aws\ApiGatewayManagementApi\ApiGatewayManagementApiClient;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Config;
use Laravel\Reverb\Loggers\Log;
use Throwable;

class SendToConnection
class SendToConnection implements ShouldQueue
{
use Dispatchable;

Expand All @@ -25,11 +26,6 @@ public function __construct(public string $connectionId, public string $message)
*/
public function handle(): void
{
info('Sending message to connection.', [
'connectionId' => $this->connectionId,
'message' => $this->message,
]);

try {
$client = new ApiGatewayManagementApiClient([
'region' => Config::get('reverb.servers.api_gateway.region'),
Expand Down
11 changes: 4 additions & 7 deletions src/Servers/ApiGateway/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@ public function handle(Request $request): void
$this->connect($request),
),
'MESSAGE' => $this->server->message(
$this->connect($request),
$connection = $request->message()
$connection = $this->connect($request),
$request->message()
),
};
} catch (InvalidApplication $e) {
app()->terminating(fn () => SendToConnection::dispatch(
$request->connectionId(),
$e->getMessage()
));
SendToConnection::dispatch($request->connectionId(), $e->getMessage());
} catch (\Exception $e) {
$this->server->error(
$this->connect($request),
Expand All @@ -53,7 +50,7 @@ public function handle(Request $request): void
}

if (isset($connection)) {
app()->terminating(fn () => app(ConnectionManager::class)->update($connection));
app(ConnectionManager::class)->update($connection);
}
}

Expand Down

0 comments on commit 3c288ff

Please sign in to comment.