无法使用 Laravel Echo、laravel-echo-server 和 socket.io 进行广播
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40049438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Broadcasting with Laravel Echo, laravel-echo-server and socket.io won't work
提问by halloei
I've setup websockets with Laravel sucessfully with an own implementation. Now I'd like to switch to Laravel Echo and laravel-echo-server. But, after many hours of trying and reading every piece of documentation I could find, I do need further help.
我已经使用自己的实现成功地使用 Laravel 设置了 websockets。现在我想切换到 Laravel Echo 和laravel-echo-server。但是,经过数小时的尝试和阅读我能找到的每一份文档后,我确实需要进一步的帮助。
What's happening when I fire an event: The queue worker processes the event as desired: "Processed: Illuminate\Broadcasting\BroadcastEvent". But nothing happens in the laravel-echo-server console nor on the client.
当我触发事件时发生了什么:队列工作人员根据需要处理事件:“已处理:Illuminate\Broadcasting\BroadcastEvent”。但是在 laravel-echo-server 控制台和客户端上都没有任何反应。
Some information:
一些信息:
Laravel is running on port 8001
Redis is running on 6379
the queue worker is running (php artisan queue:work)
Laravel, Redis and laravel-echo-server are running on the same machine (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)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 运行在 8001 端口
Redis 在 6379 上运行
队列工作者正在运行(php artisan queue:work)
Laravel、Redis 和 laravel-echo-server 运行在同一台机器上 (192.168.134.214)
尝试私人频道时,身份验证
BroadcastServiceProvider
似乎有效(如果失败,服务器会将错误写入控制台)客户端使用 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
应用程序.js
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: 'socket.io',
host: 'http://192.168.134.214:6001',
});
app.blade.php
应用程序.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());
I was hoping to get some information out of the laravel-echo-server when switching "devMode" to true. But that doesn't seem to change anything!
我希望在将 "devMode" 切换为true时从 laravel-echo-server 中获取一些信息。但这似乎并没有改变任何事情!
回答by Richard Brown
In your redis configuration, you've included http://as a protocol with the host. You will need to remove the protocol and simply use localhostor 127.0.0.1
在您的 redis 配置中,您已将http://作为与主机的协议包含在内。您将需要删除协议并简单地使用localhost或127.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"; that's moreso about performance but I'm sure this correction should get it working.
*旁白:如果您有能力使用套接字,请考虑使用 "path": "/path/to/sockfile" 代替 "host";这更多是关于性能,但我相信这个更正应该让它工作。
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
}