【问题标题】:Pusher Receive Data NullPusher 接收数据空
【发布时间】:2020-09-05 17:34:31
【问题描述】:

我使用的是 laravel 5.8 版本。我正在向推送者发送消息和用户值,但将推送者接收事件 {“消息”:空,“用户”:空}。 这是我的 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 文件。 命名空间应用\事件;

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 文件。

import Echo from '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:'7d64c4828272fba9bbdd',
        cluster: 'ap2',
        encrypted: true
});

我向推送器发送消息和用户的控制器功能。

 public function send()
    {
        $message = 'hello';
        $user = User::find(Auth::id());

        event(new ChatEvent($message, $user));
    }
}

【问题讨论】:

    标签: laravel vue.js pusher


    【解决方案1】:

    在您的 chatEvent 类中添加有效负载:并且您正在以错误的方式访问数据成员:

     $this->message = $message; //right way
    

    代替:

    $this->$message = $message; //wrong way,(remove dollar sign when using $this->)
    

    编写代码如下:您也可以将消息变量替换为有效负载:

    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;
        public $payload;
        /**
         * Create a new event instance.
         *
         * @return void
         */
        public function __construct($message, User $user, $payload)
        {
            $this->message = $message;
             $this->user = $user;
             $this->payload = $payload;
        }
    
        /**
         * Get the channels the event should broadcast on.
         *
         * @return \Illuminate\Broadcasting\Channel|array
         */
        public function broadcastOn()
        {
            return new PrivateChannel('channel-name');
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-06
      • 2021-07-11
      • 2018-04-08
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      相关资源
      最近更新 更多