【发布时间】: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