【问题标题】:Laravel Echo is not listening to the eventsLaravel Echo 没有监听事件
【发布时间】:2022-06-21 20:46:07
【问题描述】:

我正在处理实时通知,我正在使用以下软件包: “predis/predis”:“^1.1” "laravel-echo": "^1.4.0", "io": "^1.0.0", "socket.io": "^2.1.1", "socket.io-client": "^2.4.0"

我还降级了 socke.io-client 版本并添加了点“。”在事件名称之前。但是我的前端没有得到任何回应。谁能帮帮我

我的 laravel-echo-server 配置是

{
    "authHost": "http://localhost:8000",
    "authEndpoint": "/broadcasting/auth",
    "clients": [],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "",
        "allowMethods": "",
        "allowHeaders": ""
    }
}

我的 .env 文件是

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

我的测试事件

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NotificationEvent implements ShouldBroadcastNow 
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $message ;
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('notification-event');
    }

    // public function broadcastAs()
    // {
    //     return 'notification-event';
    // }
}

Bootstrap.js

window.axios = require('axios');
import Echo from "laravel-echo"
window.io = require('socket.io-client');

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});

还有我的前端

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel</title>
        <meta name="csrf-token" content="{{ csrf_token() }}">
    </head>
    <body>

      <script src="{{asset('js\app.js')}}"></script>
      <!-- <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> -->
      <script>
        Echo.channel('notification-event')
        .listen('.NotificationEvent', (e) => {
            swal('Notification', e.message);
            });
    </script>
    </body>
</html>

路线

Route::get('send_test_event', function () {
        event(new App\Events\NotificationEvent('Is online now'));
        return "Event has been sent!";
    });

这是 laravel-echo-server 的输出 enter image description here

【问题讨论】:

    标签: laravel redis socket.io laravel-echo


    【解决方案1】:

    进行这些更改,看看它是否有效

    .ENV 文件

    BROADCAST_DRIVER=redis QUEUE_CONNECTION=redis

    在 Bootstrap.js 中

    import Echo from "laravel-echo"
    window.io = require('socket.io-client');
    
    
    window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
    withCredentials: true
    
    });
    

    在 echo-server 配置中

    "socketio": {
        "cors": {
            "origin": "http://localhost",
            "credentials": true
        }
    },
    

    你应该在触发事件之前运行 php artisan queue:listen 或 queue:work

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2017-08-21
      • 2020-11-03
      • 2021-05-02
      • 2020-08-22
      • 2018-03-18
      • 1970-01-01
      相关资源
      最近更新 更多