我正在使用Laravel 5.6和socket.io
在我的应用程序的管理面板中广播消息。
在我的应用程序中,需要在将errors and warnings
登录到admin之前广播生成的所有laravel logs files
。
我只想知道如何实现这一功能。自从我成为Laravel框架的新手以来,还有哪些简单的方法。
这是我到目前为止所做的:
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>
这就是我如何修改我的app \ 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);
}
在你的
应用程序/异常/ Handler.php
在render类中的这个类中你可以
public function report(Exception $exception)
{
broadcast($yourEvent)
parent::report($exception);
}