推送器接收具有空值的事件
Pusher receive event with null values
我正在使用 laravel 5.8 版本。我正在向推送器发送消息,但让推送器接收事件 {"message": null}。
这是我的 broadcasting.php 文件
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
// 'cluster' => env('PUSHER_APP_CLUSTER'),
// 'useTLS' => true,
'cluster' => 'ap2',
'useTLS' => true
],
],
有我的 chatevent.php 文件。
namespace App\Events
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Auth\User;
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 ChatEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public $user;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message, User $user)
{
$this->$message = $message;
$this->$user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
这是我的 bootstrap.js 文件。
从 'laravel-echo';
导入回声
window.Pusher = require('pusher-js');
window.Echo = new Echo({
// broadcaster: 'pusher',
// key: process.env.MIX_PUSHER_APP_KEY,
// cluster: process.env.MIX_PUSHER_APP_CLUSTER,
// encrypted: truebroadcaster: 'pusher',
broadcaster: 'pusher',
key:'REDACTED',
cluster: 'ap2',
encrypted: true
});
我向推送器发送消息的控制器功能。
public function send()
{
$message = 'hello';
$user = User::find(Auth::id());
event(new ChatEvent($message, $user));
}
}
如何解决????
我认为您编写事件的构造函数 class 有误。
在这里,您在 message
和 user
变量
之前放置了额外的 $
public function __construct($message, User $user)
{
$this->$message = $message;
$this->$user = $user;
}
应该像下面这样
public function __construct($message, User $user)
{
$this->message = $message;
$this->user = $user;
}
我正在使用 laravel 5.8 版本。我正在向推送器发送消息,但让推送器接收事件 {"message": null}。 这是我的 broadcasting.php 文件
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
// 'cluster' => env('PUSHER_APP_CLUSTER'),
// 'useTLS' => true,
'cluster' => 'ap2',
'useTLS' => true
],
],
有我的 chatevent.php 文件。
namespace App\Events
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Auth\User;
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 ChatEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public $user;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message, User $user)
{
$this->$message = $message;
$this->$user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
这是我的 bootstrap.js 文件。 从 'laravel-echo';
导入回声window.Pusher = require('pusher-js');
window.Echo = new Echo({
// broadcaster: 'pusher',
// key: process.env.MIX_PUSHER_APP_KEY,
// cluster: process.env.MIX_PUSHER_APP_CLUSTER,
// encrypted: truebroadcaster: 'pusher',
broadcaster: 'pusher',
key:'REDACTED',
cluster: 'ap2',
encrypted: true
});
我向推送器发送消息的控制器功能。
public function send()
{
$message = 'hello';
$user = User::find(Auth::id());
event(new ChatEvent($message, $user));
}
}
如何解决????
我认为您编写事件的构造函数 class 有误。
在这里,您在 message
和 user
变量
$
public function __construct($message, User $user)
{
$this->$message = $message;
$this->$user = $user;
}
应该像下面这样
public function __construct($message, User $user)
{
$this->message = $message;
$this->user = $user;
}