Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Added new events #91

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Events/Concerns/Dispatchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Laravel\Reverb\Events\Concerns;

use Laravel\Reverb\Loggers\Log;

trait Dispatchable
{
/**
* Dispatch the event with the given arguments.
*
* @return mixed
*/
public static function dispatch()
{
try {
return event(new static(...func_get_args()));
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
}
}
19 changes: 19 additions & 0 deletions src/Events/ConnectionClosed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class ConnectionClosed
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(public Connection $connection)
{
//
}
}
19 changes: 19 additions & 0 deletions src/Events/ConnectionError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class ConnectionError
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(public Connection $connection, public \Exception $exception)
{
//
}
}
2 changes: 1 addition & 1 deletion src/Events/MessageReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Laravel\Reverb\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class MessageReceived
{
Expand Down
2 changes: 1 addition & 1 deletion src/Events/MessageSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Laravel\Reverb\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class MessageSent
{
Expand Down
19 changes: 19 additions & 0 deletions src/Events/NewConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class NewConnection
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(public Connection $connection)
{
//
}
}
18 changes: 18 additions & 0 deletions src/Events/ServerStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Events\Concerns\Dispatchable;

class ServerStarted
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}
}
18 changes: 18 additions & 0 deletions src/Events/ServerStopped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Events\Concerns\Dispatchable;

class ServerStopped
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}
}
22 changes: 22 additions & 0 deletions src/Events/SubscribedToChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class SubscribedToChannel
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(
public Connection $connection,
public string $channelName,
public ?string $auth = null,
) {
//
}
}
21 changes: 21 additions & 0 deletions src/Events/UnsubscribedFromChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class UnsubscribedFromChannel
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(
public Connection $connection,
public string $channelName,
) {
//
}
}
6 changes: 6 additions & 0 deletions src/Protocols/Pusher/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Laravel\Reverb\Protocols\Pusher\Channels;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\SubscribedToChannel;
use Laravel\Reverb\Events\UnsubscribedFromChannel;
use Laravel\Reverb\Protocols\Pusher\Concerns\SerializesChannels;
use Laravel\Reverb\Protocols\Pusher\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Protocols\Pusher\Contracts\ChannelManager;
Expand Down Expand Up @@ -66,6 +68,8 @@ public function findById(string $id): ?Connection
public function subscribe(Connection $connection, ?string $auth = null, ?string $data = null): void
{
$this->connections->add($connection, $data ? json_decode($data, true) : []);

SubscribedToChannel::dispatch($connection, $this->name(), $auth);
}

/**
Expand All @@ -78,6 +82,8 @@ public function unsubscribe(Connection $connection): void
if ($this->connections->isEmpty()) {
app(ChannelManager::class)->for($connection->app())->remove($this);
}

UnsubscribedFromChannel::dispatch($connection, $this->name());
}

/**
Expand Down
31 changes: 19 additions & 12 deletions src/Protocols/Pusher/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Exception;
use Illuminate\Support\Str;
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\ConnectionClosed;
use Laravel\Reverb\Events\ConnectionError;
use Laravel\Reverb\Events\MessageReceived;
use Laravel\Reverb\Events\NewConnection;
use Laravel\Reverb\Loggers\Log;
use Laravel\Reverb\Protocols\Pusher\Contracts\ChannelManager;
use Laravel\Reverb\Protocols\Pusher\Exceptions\InvalidOrigin;
Expand Down Expand Up @@ -34,6 +37,8 @@ public function open(Connection $connection): void
$this->handler->handle($connection, 'pusher:connection_established');

Log::info('Connection Established', $connection->id());

NewConnection::dispatch($connection);
} catch (Exception $e) {
$this->error($connection, $e);
}
Expand Down Expand Up @@ -82,6 +87,8 @@ public function close(Connection $connection): void
$connection->disconnect();

Log::info('Connection Closed', $connection->id());

ConnectionClosed::dispatch($connection);
}

/**
Expand All @@ -93,21 +100,21 @@ public function error(Connection $connection, Exception $exception): void
$connection->send(json_encode($exception->payload()));

Log::error('Message from '.$connection->id().' resulted in a pusher error');
Log::info($exception->getMessage());

return;
} else {
$connection->send(json_encode([
'event' => 'pusher:error',
'data' => json_encode([
'code' => 4200,
'message' => 'Invalid message format',
]),
]));

Log::error('Message from '.$connection->id().' resulted in an unknown error');
}

$connection->send(json_encode([
'event' => 'pusher:error',
'data' => json_encode([
'code' => 4200,
'message' => 'Invalid message format',
]),
]));

Log::error('Message from '.$connection->id().' resulted in an unknown error');
Log::info($exception->getMessage());

ConnectionError::dispatch($connection, $exception);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Servers/Reverb/Console/Commands/StartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Laravel\Reverb\Application;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\Logger;
use Laravel\Reverb\Events\ServerStarted;
use Laravel\Reverb\Events\ServerStopped;
use Laravel\Reverb\Jobs\PingInactiveConnections;
use Laravel\Reverb\Jobs\PruneStaleConnections;
use Laravel\Reverb\Loggers\CliLogger;
Expand Down Expand Up @@ -68,6 +70,8 @@ public function handle(): void
$this->components->info('Starting '.($server->isSecure() ? 'secure ' : '')."server on {$host}:{$port}".(($hostname && $hostname !== $host) ? " ({$hostname})" : ''));

$server->start();

ServerStarted::dispatch();
}

/**
Expand Down Expand Up @@ -108,6 +112,8 @@ protected function ensureRestartCommandIsRespected(Server $server, LoopInterface

$server->stop();

ServerStopped::dispatch();

$this->components->info("Stopping server on {$host}:{$port}");
});
}
Expand Down