没有收到推送消息

时间:2020-04-03 22:07:26

标签: php laravel vue.js pusher broadcasting

您好,我正在使用laravel broadcastingpusher发送一些信息。
我可以成功地向推送器发送消息 ,但是 不能接收
这是我的代码,请帮助我:(

App.js

require('./bootstrap');
window.Vue = require('vue');

const app = new Vue({
    el: "#app",
    created() {
        Echo.private('gameRoom')
            .listen('RequestsEvent', (e) => {
                console.log(e);
            });
    }
});

Bootstrap.js

window._ = require('lodash');

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

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,
    forceTLS: true
});

RequestsEvent.php

class RequestsEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $type;
    public $room_manager;

    /**
     * Create a new event instance.
     *
     * @param $type
     * @param $room_manager
     */
    public function __construct($type, $room_manager)
    {
        $this->type = $type;
        $this->room_manager = $room_manager;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return PrivateChannel
     */
    public function broadcastOn()
    {
        return new PrivateChannel('gameRoom');
    }
}

Channels.php

Broadcast::channel('gameRoom', function () {
    return true;
});

HTML:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Listen</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="{{ asset('css/app.css') }}"/>
</head>
<body>
<div id="app"></div>

<script type="text/javascript" src="{{ asset('js/app.js') }}"></script>
</body>
</html>

我已经安装了pusherlaravel echo,并且我的console中有以下内容:

下载Vue Devtools扩展以进行更好的开发 经验:https://github.com/vuejs/vue-devtools app.js:38040
您正在开发模式下运行Vue。确保打开生产模式 在部署生产时。请参阅以下更多技巧 https://vuejs.org/guide/deployment.html

2 个答案:

答案 0 :(得分:0)

您应该在config / app.php文件中取消注释此提供程序。像这样

'providers' => [
    ...
    App\Providers\BroadcastServiceProvider::class,
    ...
]

答案 1 :(得分:0)

大约是Vue。尽管我这样做了,但我还是在cmd中运行npm install vue,一切正常。