我试图通过和Pusher制作客户支持图,但在我的控制台中,出现了一些错误。我无法理解它。
这是控制台:

和,

这是我在Js 文件中的内容。
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.d8649589aa930d275ffe,
cluster: process.env.ap2,
encrypted: true
});
发布于 2018-06-20 17:38:09
创建事件抽象类
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;
}
}现在在拉勒维尔那边的任何地方都有这样的广播
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
https://stackoverflow.com/questions/50953725
复制相似问题