laravel56 广播系统

本文详细介绍了Laravel 5.6的广播系统,包括events事件的使用,相关路由设置,bootstrap.js的配置以及界面实现。同时推荐了两篇参考文档,帮助读者深入理解 Laravel Echo 的应用。
摘要由CSDN通过智能技术生成

events事件:

<?php

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 News implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;    
    public $token;
    protected $channel;

/*  public function __construct()
    {
    }

    public function broadcastOn()
    {
        return new PrivateChannel('news');
    }*/
    /**
     * Create a new event instance.
     *
     * @param  string $token
     * @param  string $channel
     * @return void
     */
    public $id;
    public $msg;    
    public $userId; 
    public function __construct($id,$userId)
    {
/*        $this->token = $token;
        $this->channel = $channel;*/
        $this->id = $id;
        $this->userId = $userId;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        //return [$this->channel];
        //公有
        //return new Channel('News');
        //私有
        return new PrivateChannel('News-'. $this->userId);
    }

    /**
     * Get the name the event should be broadcast on.
     *
     * @return string
     */
/*    public function broadcastAs()
    {
        //return 'dlogin';
    } */
    public function broadcastWith()
    {
        // 返回当前时间
        return ['name' => 'asfdfa'];
    }       
}

路由:

Route::get('/ship', function (Request $request,Schedule $schedule)
{
    $id = $request->input('id');
    event(new News($id)); // 触发事件
    return Response::make('Order Shipped!');
});

bootstrap.js

import Echo from 'laravel-echo'
 
window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://localhost:6001'
});

 

界面:

<!-- 文件保存于 resources/views/layouts/child.blade.php -->
<html>
<head>
<meta name="csrf-token" content="{{ csrf_token() }}">	
</head>
<script src="http://localhost:6001/socket.io/socket.io.js"></script>
<body>
<div id='app'></div>
	<div>sdfsf</div>
<script src="{{asset('/js/app.js') }}"></script>
</body>	
<script>
    //console.log(Echo);
    //公有
    //Echo.channel('News')
    //私有
    Echo.private('News')
        .listen('News', (e) => {
            // 如果有广播过来你可以进行逻辑操作,比如给用户一个通知
            console.log(e.name)
        });
</script>        
</html>


两篇很好的参考文档:

https://blog.csdn.net/xinzi11243094/article/details/80186503

https://laravel-china.org/index.php/articles/15031/simple-16-steps-to-go-through-the-use-of-laravel-echo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值