5

我正在使用 Laravel 开发实时应用程序。由于我不想使用 Pusher,我正在尝试使用Laravel Echo Server、Redis 和 Socket.IO 使 websockets 工作。但是,我遇到了这个堆栈的问题。客户端(浏览器)似乎无法从事件中接收数据。我还在前端使用 Intertia 和 Vue。还值得注意的是,我使用Laragon作为我的本地开发环境。

我的.env,有些被省略了:

APP_URL=http://malbe.test

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

MIX_APP_URL="${APP_URL}"

我的laravel-echo-server.json

{
    "authHost": "http://malbe.test",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "omitted",
            "key": "omitted"
        }
    ],
    "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": true,
        "allowOrigin": "http://malbe.test:80",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

我的 redis 配置database.php:注意:我使用它是因为即使我在 PHP 扩展上安装了 dll,我predis似乎也无法工作。phpredis

'redis' => [

        // 'client' => env('REDIS_CLIENT', 'phpredis'),
        'client' => env('REDIS_CLIENT', 'predis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            // 'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
            'prefix' => "",
        ],

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
        ],

        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_CACHE_DB', '1'),
        ],

    ],

我的 Laravel 活动:

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

   public function __construct()
   {
       //
   }

   public function broadcastOn() {
      return new Channel("my-channel");
   }

   public function broadcastWith() {
      return ["data"=>"Hello"];
   }
}

我的回声初始化在bootstrap.js

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

if (typeof window.io !== "undefined") {
    window.Echo = new Echo({
        broadcaster: "socket.io",
        host: process.env.MIX_APP_URL + ":6001"
    });
}

在订阅 Echo 服务器的 Vue 页面上

export default {
    props: ["foo"],
    mounted: function () {
        window.Echo.channel("my-channel").listen("SampleEvent", (d) => console.log(d));
    }
};

运行这 3 个命令:

  1. laravel-echo-server start
  2. npm run watch
  3. php artisan queue:work

我尝试使用 Tinker 和 GET 请求广播事件。

Route::get("/broadcast", function () {
    event(new \App\Events\SampleEvent);
    // also tried
    // broadcast(new \App\Events\SampleEvent);
});

队列工作者处理事件;

[2020-12-24 11:07:35][n9FKq0n6C6QOKa2f1JBzWcCqgYqLiDvU] Processing: App\Events\SampleEvent
[2020-12-24 11:07:35][n9FKq0n6C6QOKa2f1JBzWcCqgYqLiDvU] Processed:  App\Events\SampleEvent

laravel-echo-server在广播事件时输出;

L A R A V E L  E C H O  S E R V E R  
                                     
version 1.6.2                        
                                     
⚠ Starting server in DEV mode...     
                                     
✔  Running at localhost on port 6001 
✔  Channels are ready.               
✔  Listening for http events...      
✔  Listening for redis events...     
                                     
Server ready!                        
                                 
Channel: my-channel                  
Event: App\Events\SampleEvent        

客户端没有显示任何错误。以下是开发者控制台上的网络选项卡:

浏览器网络选项卡

当我关闭laravel-echo-server客户端仍然打开时,每隔一秒就会发生此错误:

ERR_CONNECTION_REFUSED

抱歉这个冗长的问题,我只需要显示我的所有配置,以便我可以帮助您解决这个问题。谁能指出我正确的解决方案?谢谢!

4

1 回答 1

13

我认为您的配置代码是正确的。package.json您可以在 vue 中看到您的配置:

"socket.io-client": "^2.4.0",

如果你"socket.io-client">=3,你最好把它改成2.4.0. 并运行:

sudo cnpm i socket.io@2.4.0
于 2021-01-23T13:07:41.043 回答