【问题标题】:How to broadcast errors and warnings in laravel?如何在 laravel 中广播错误和警告?
【发布时间】:2018-09-05 09:19:29
【问题描述】:

我正在使用 Laravel 5.6 和 socket.io 在我的应用程序的管理面板中广播消息。

在我的应用程序中,需要广播所有生成的errors and warnings,然后再将它们登录到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);
    }

【问题讨论】:

    标签: php laravel socket.io broadcast phpwebsocket


    【解决方案1】:

    在你的

    app/Exceptions/Handler.php

    在这个类里面的render方法可以

     public function report(Exception $exception)
        {
    
            broadcast($yourEvent)
            parent::report($exception);
        }
    

    【讨论】:

    • @DineshSuthar 是否存在您尝试广播的课程?
    • 它给出控制台错误:不允许序列化'Closure'
    • 是的,该类存在于 App\Events\ErrorBroadcasting 中。我的频道目前是公开的,所以我认为没有必要在channel.php创建频道条目
    • @DineshSuthar 你在用 pusher 吗?
    • 不,我没有使用 pusher,我使用 redis 和 socket.io 进行广播
    猜你喜欢
    • 2017-12-26
    • 2018-09-22
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2017-12-12
    • 2015-08-29
    • 2023-03-17
    • 2016-07-28
    相关资源
    最近更新 更多