Laravel客户支持与第三方Pusher聊天

时间:2018-06-20 17:15:57

标签: laravel vue.js websocket pusher

我试图在laravelPusher上制作客户支持图表,但是在我的控制台中,出现了一些错误。我无法理解。

这是控制台: enter image description here

然后

enter image description here

这就是我在Js 文件

中所拥有的

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.d8649589aa930d275ffe,
    cluster: process.env.ap2,
    encrypted: true
});

1 个答案:

答案 0 :(得分:1)

  

创建事件摘要类

namespace App\Events;

use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;

abstract class Event implements ShouldBroadcast
{
    use SerializesModels;

    protected $message;
    protected $channels = [];
    protected $event = '';

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return $this->channels;
    }

    /**
     * Broadcast to event
     */
    public function broadcastAs()
    {
        return $this->event;
    }

    /**
     * Broadcast with message
     */
    public function broadcastWith()
    {
        return  $this->message;
    }
}
  

现在创建PusherEvent类

namespace App\Events;

class PusherEvent extends Event
{

    /**
     * Create a new event instance.
     * @param $message
     * @return $this
     */
    public function setMessage($message)
    {
        $this->message = $message;
        return $this;
    }

    /**
     * Set pusher channels
     */
    public function setChannels($channels)
    {
        $this->channels = $channels;
        return $this;
    }

    /**
     * Set pusher event
     */
    public function setEvent($event)
    {
        $this->event = $event;
        return $this;
    }
}
  

现在可以从Laravel Side的任何地方广播

    try {
            $messageEvent = new PusherEvent();
            $messageEvent->setEvent('chatMessage')
                        ->setChannels(['customerChat']);
            $params['message'] = "Hello From Laravel";
            event($messageEvent->setMessage($params));
        } catch (\Exception $e) {
            customLog($e->getMessage());
            return false;
        }

调用此代码后,您可以检查推入式控制台。您将获得“来自Laravel的问候”

这是一个有效的代码。我已经在我的项目中使用过。希望对您有帮助。

  

用于从js列出

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'd8649589aa930d275ffe',
    cluster: 'ap2',
    encrypted: true
});

Echo.channel('customerChat')
    .listen('chatMessage', (e) => {
        console.log(e.message);
    });

有关详细信息,https://laravel.com/docs/5.6/broadcasting#concept-overview