首页 > 解决方案 > Laravel5.8 推送器不工作。收不到

问题描述

我正在创建实时聊天应用程序。我已经在我的 laravel 和 vue.js 项目中设置了 pusher。

但它不起作用。虽然我在控制台中没有任何错误。另外,我在网络选项卡中没有错误。

我需要创建信使应用程序,所以我需要一个实时聊天功能。现在,我可以推送用户的评论,但在其他用户的窗口中,什么也没有显示。

但它确实如此,一旦我刷新页面。我认为我的推送器设置有问题,因为在推送器调试控制台中,没有执行任何会话。

这是我的代码。.env

BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

PUSHER_APP_ID=my id
PUSHER_APP_KEY=my app key
PUSHER_APP_SECRET= my secret key
PUSHER_APP_CLUSTER=mt1

广播.php

'pusher' => [
            'driver' => 'pusher',
            'key' => env('my key'),
            'secret' => env('my secret key'),
            'app_id' => env('my id'),
            'options' => [
                'cluster' => 'ap3',
                'encrypted' => true,
            ],

广播服务提供者.php

Broadcast::routes(['middleware' => ['auth:api']]);
        require base_path('routes/channels.php');

引导程序.js

import Echo from 'laravel-echo'

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

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

新消息.php

use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class NewMessage implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $message;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Message $message)
    {
        $this->message = $message;
    }
    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('messages.' . $this->message->to);
    }
    public function broadcastWith()
    {
        $this->message->load('fromContact');
        return ["message" => $this->message];
    }
}

路线/通道.php

use Illuminate\Support\Facades\Broadcast;

联系人控制器.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Message;

class ContactsController extends Controller
{
    public function get() {
        $contacts = User::where('id', '!=', auth()->id())->get();//全部のcontactをjson経由でgetする

        return response()->json($contacts);    
    }

    public function getMessagesFor($id) {
        $messages = Message::where('from', $id)->orWhere('to', $id)->get();
        return response() -> json($messages);
    }

    public function send(Request $request) {
        $message = Message::create([
            'from' => auth()->id(),
            'to' => $request->contact_id,
            'text' => $request->text
        ]);



        return response()->json($message);
    }
}

这是我尝试过的。

根据 laravel 文档运行所有命令。

php artisan chache:clear 并再次运行服务器。

运行php artisan queue:work 在命令终端

标签: phplaravelvue.jslaravel-5pusher

解决方案


你写了吗

window.Echo.private('channelName').listen('EventName',function(e){
})

在您的应用程序中收听


推荐阅读