本文介绍了Laravel5.8推杆无法正常工作.无法接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating realtime chat app.I have set up pusher in my laravel and vue.js project.

但是它不起作用.虽然我在控制台中没有任何错误.另外,我在网络"标签中没有任何错误.

But it doesn't work. Though I don't have any error in the console.Also, I have no error in network tab.

我需要创建Messenger应用,所以我需要一个实时聊天功能.现在,我可以推送用户的评论,但是在其他用户的窗口中,什么都没有显示.

I need to create messenger app, so I need a realtime chat function.Now, I can push user's comment but in the other user's window, nothing shows up.

但是确实如此,一旦刷新页面.我认为我的pusher设置有问题,因为在pusher调试控制台中,不会执行任何会话.

But it does, once I refresh the page. I think my pusher setting has something wrong, because in the pusher debug console, any session is not executed.

这是我的代码..env

Here is my code..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

broadcasting.php

broadcasting.php

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

BroadcastServiceProvider.php

BroadcastServiceProvider.php

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

bootstrap.js

bootstrap.js

import Echo from 'laravel-echo'

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

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

NewMessage.php

NewMessage.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];
    }
}

routes/channel.php

routes/channel.php

use Illuminate\Support\Facades\Broadcast;

ContactsController.php

ContactsController.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文档运行所有命令.

run all the command according to the laravel document.

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

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

推荐答案

您写了

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

在您的应用程序中收听

这篇关于Laravel5.8推杆无法正常工作.无法接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:03