Я использую Laravel 5.6 и socket.io
для трансляции сообщений в административной панели моего приложения.
В моем приложении есть требование транслировать все сгенерированные errors and warnings
, прежде чем регистрировать их в laravel logs files
для администратора.
Я просто хотел узнать, как мне добиться этой функциональности. Каковы другие простые способы, так как я новичок в Laravel Framework.
Это то, что я сделал до сих пор:
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class ErrorBroadcasting implements ShouldBroadcast
{
use InteractsWithSockets, SerializesModels;
public $errorContent;
public function __construct($errorContent)
{
$this->errorContent = $errorContent;
}
public function broadcastOn()
{
return new PresenceChannel('error-broadcasting-channel');
}
}
И вот как я потребляю канал в моем клинке.
<script>
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
});
var buyer = Echo.channel(`error-broadcasting-channel`);
buyer.listen("ErrorBroadcasting", e => {
$('#error_container').append("<div class=\"row chat-snippet\">\n" +
" <div class=\"col-lg-12\">\n" +
" <small>Buyer:</small>\n" +
" <p>"+e.errorContent+"</p>\n" +
" </div>\n" +
" </div>");
});
</script>
И вот как я изменил свое приложение \ Exceptions \ Handler.php
public function report(Exception $exception)
{
if (config('app.mail_exception') && $this->shouldReport($exception)) {
$this->sendEmail($exception); // sends an email
}
broadcast(new ErrorBroadcasting($exception))->toOthers();
parent::report($exception);
}