首页 > 解决方案 > Laravel 与 Vuejs 和 Redis , laravel-echo , socketio-client

问题描述

我正在将 laravel 8 与 vuejs 3 (SPA) 一起使用,并希望通过 redis、laravel echo 和 socketio 客户端实时使用应用程序。

如果我没记错 laravel server <-> redis server <-> laravel-sech-server<-> socketio-client-server,流程是这样的。

但是当我运行 laravel-eacho-server start 它给出了这个错误我认为 redis 配置文件有问题。 这是它显示的错误,但控制台 应用程序/事件文件中没有错误我有 TestEvent

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
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;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

class TestEvent implements ShouldBroadcastNow
{
    use InteractsWithSockets, SerializesModels;

    public function __construct()
    {

    }
    public function broadcastAs()
    {
        return 'UserEvent';
    }
    public function broadcastWith()
    {
        return ['title'=>'some notification'];
    }
    public function broadcastOn()
    {
        return new Channel('test');
    }
}

在主 vuejs 文件中,它是 root 的子级。在创造它的功能

created(){
        Echo.channel('test')
            .listen('TestEvent', e => {
                console.log(e)
            });
    },

在 bootstrap.js 文件中(laravel-echo 服务器和 socketio-client 服务器之间的连接~right)

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

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001' // this is laravel-echo-server host
});

在 .evn 文件中


BROADCAST_DRIVER=redis
CACHE_DRIVER=file
QUEUE_CONNECTION=redis
QUEUE_DRIVER=redis
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

LARAVEL_ECHO_PORT=6001

REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

最后是 laravel-echo-server.json

{
    "authHost": "http://localhost",
    "authEndpoint": "/api/broadcasting/auth",
    "clients": [],
    "database": "redis",
    "databaseConfig": {
        "redis": {

        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "secureOptions": 67108864,
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "",
        "allowMethods": "",
        "allowHeaders": ""
    }
}

我也在运行 redis 服务器 redis-server.exe

如果可能的话,如果有任何关于此事的材料可以分享,我们将不胜感激。它也可以运行

laravel 服务器 <-> redis 服务器 <-> nodejs-server (socketio-server) <-> socketio-client-server. (找不到这种流程的任何示例)。

标签: laravelredissocket.iolaravel-echo

解决方案


推荐阅读