简体   繁体   English

使用Laravel Echo,laravel-echo-server和socket.io进行广播无法正常工作

[英]Broadcasting with Laravel Echo, laravel-echo-server and socket.io won't work

I've setup websockets with Laravel sucessfully with an own implementation. 我已经使用自己的实现成功地使用Laravel设置了websockets。 Now I'd like to switch to Laravel Echo and laravel-echo-server . 现在,我想切换到Laravel Echo和laravel-echo-server But, after many hours of trying and reading every piece of documentation I could find, I do need further help. 但是,经过许多小时的尝试并阅读了我能找到的所有文档后,我确实需要进一步的帮助。

What's happening when I fire an event: The queue worker processes the event as desired: " Processed: Illuminate\\Broadcasting\\BroadcastEvent ". 触发事件时发生了什么:队列工作器根据需要处理事件:“已处理:Illuminate \\ Broadcasting \\ BroadcastEvent ”。 But nothing happens in the laravel-echo-server console nor on the client. 但是,在laravel-echo-server控制台或客户端上都没有任何反应。

Some information: 一些信息:

  • Laravel is running on port 8001 Laravel在端口8001上运行

  • Redis is running on 6379 Redis在6379上运行

  • the queue worker is running (php artisan queue:work) 队列工作者正在运行(php artisan queue:work)

  • Laravel, Redis and laravel-echo-server are running on the same machine (192.168.134.214) Laravel,Redis和laravel-echo-server在同一台机器上运行(192.168.134.214)

  • when trying private channels the authentication in the BroadcastServiceProvider seems to work (the server writes errors to the console if it fails) 尝试专用频道时, BroadcastServiceProvider的身份验证似乎可以正常工作(如果服务器失败,服务器会将错误写入控制台)

  • the client uses the socket.io script of the laravel-echo-server: <script src="//192.168.134.214:6001/socket.io/socket.io.js"></script> 客户端使用laravel-echo-server的socket.io脚本: <script src="//192.168.134.214:6001/socket.io/socket.io.js"></script>

Excerpt of my .env: 我的.env摘录:

BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_DRIVER=redis

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

The laravel-echo-server.json: laravel-echo-server.json:

{
    "appKey": "<myAppKey>",
    "authEndpoint": "/broadcasting/auth",
    "authHost": "http://localhost:8001",
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "http://localhost"
        }
    },
    "devMode": true,
    "host": "192.168.134.214",
    "port": "6001",
    "sslCertPath": "",
    "sslKeyPath": ""
}

app.js app.js

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://192.168.134.214:6001',
});

app.blade.php app.blade.php

<script>
    Echo.channel('mychan')
        .listen('myevent', (e) => {
            console.log('Hello World!', e);
        });
</script>

Parts of the Event "NewsPublished": 事件“ NewsPublished”的部分内容:

class NewsPublished implements ShouldBroadcast {
    use InteractsWithSockets, SerializesModels;

    public function broadcastOn() {
        return new Channel('mychan');
    }

    public function broadcastAs() {
        return 'myevent';
    }
}

..and I'm firing the Event with event(new App\\Events\\NewsPublished()); ..并且我用event(new App\\Events\\NewsPublished());触发event(new App\\Events\\NewsPublished());

I was hoping to get some information out of the laravel-echo-server when switching "devMode" to true . 当将“ devMode”切换为true时,我希望从laravel-echo-server中获得一些信息。 But that doesn't seem to change anything! 但这似乎并没有改变任何东西!

In your redis configuration, you've included http:// as a protocol with the host. 在redis配置中,您已将http://作为主机的协议。 You will need to remove the protocol and simply use localhost or 127.0.0.1 您将需要删除协议,只需使用localhost127.0.0.1

{
...
"databaseConfig": {
    "redis": {
        "port": "6379",
        "host": "localhost"
    }
...
}

*Aside: If you have the ability to use a socket, consider using "path": "/path/to/sockfile" in-place of "host"; *此外:如果您有使用套接字的能力,请考虑使用“ path”:“ / path / to / sockfile”代替“ host”; that's moreso about performance but I'm sure this correction should get it working. 关于性能的更多信息,但是我敢肯定,此更正应该可以使它起作用。

For reference, here's a redacted version of the laravel-echo-server.json configuration that I am using. 作为参考,这是我使用的laravel-echo-server.json配置的简化版本。

{
  "appKey": [omitted],
  "authEndpoint": "/broadcasting/auth",
  "authHost": "http://localhost",
  "database": "redis",
  "databaseConfig": {
    "redis": {
      "db": 2, /*this is an intentional change; the default is zero(0)*/
      "path": "/tmp/redis.sock",
      "password": [omitted]
    }
  },
  "devMode": false,
  "host": "",
  "port": "6001",
  "referrers": [
    {
      "host": "*", /*matches any referrer*/
      "apiKey": [omitted]
    }
  ],
  "sslCertPath": "",
  "sslKeyPath": "",
  "verifyAuthPath": true,
  "verifyAuthServer": false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM