Laravel 与 socket.io 的回声

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41629328/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:06:54  来源:igfitidea点击:

Laravel echo with socket.io

laravelwebsocketsocket.iolaravel-5.3laravel-echo

提问by Parth Vora

I want to listen for server side event on javascript side. I found this package for that:

我想在 javascript 端监听服务器端事件。我为此找到了这个包:

https://github.com/tlaverdure/laravel-echo-server

https://github.com/tlaverdure/laravel-echo-server

I have already read all if this so many times:

我已经读了这么多遍了:

https://laravel.com/docs/5.3/events

https://laravel.com/docs/5.3/events

https://laravel.com/docs/5.3/broadcasting

https://laravel.com/docs/5.3/broadcasting

https://laravel.com/docs/5.3/notifications

https://laravel.com/docs/5.3/notifications

So far I have done this:

到目前为止,我已经这样做了:

Controller action:

控制器动作:

broadcast(new NewVote());
event(new NewVote()); // Also tried this

"NewVote" Event class:

“NewVote”事件类:

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

Javascript:

Javascript:

import Echo from "laravel-echo"

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

window.Echo.channel('new-vote')
    .listen('NewVote', (e) => {
        console.log(e);
    });

laravel-echo-server.json

laravel-echo-server.json

{
    "appKey": "0p4o9t942ct15boc4nr8tjb178q29fdmlcat8c1p1s51i2pbq9nmtjud94t4",
    "authHost": null,
    "authEndpoint": "/broadcasting/auth",
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "assessment.local",
    "port": "6001",
    "referrers": [],
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": ""
}

Already this commands are running:

此命令已经在运行:

laravel-echo-server start
php artisan queue:work
php artisan queue:listen
gulp watch

All I'm expecting a console massage when, I fire the event. But I'm not getting anything on the console.

当我触发事件时,我期待控制台按摩。但我没有在控制台上得到任何东西。

Let me know if any other information needed.

如果需要任何其他信息,请告诉我。

Note: My socket.io server is running successfully without any error.

注意:我的 socket.io 服务器运行成功,没有任何错误。

Thanks,

谢谢,

Parth Vora

帕斯沃拉

回答by Eelke van den Bos

Configuring Echo took me a while, but I found the client side configuration to contain a confusing default event namespace.

配置 Echo 花了我一段时间,但我发现客户端配置包含一个令人困惑的默认事件命名空间。

Try configuring the Echo client side library as following:

尝试按如下方式配置 Echo 客户端库:

import Echo from "laravel-echo"

window.Echo = new Echo({
    namespace: 'Base.Event.Namespace', //defaults to App.Event
    broadcaster: 'socket.io',
    host: 'http://assessment.local:6001'
});

So when firing an event like so event(new \Hello\World\NewVote()), make sure Echo is configured with the namespace Hello.World

因此,当触发这样的事件时event(new \Hello\World\NewVote()),请确保使用命名空间配置 EchoHello.World

Cheers!

干杯!

回答by Parth Vora

Don't know what was the issue. But finally, I make it working.

不知道是什么问题。但最后,我让它工作。

I also created a small demo which illustrates the use of Laravel echo with socket.io using laravel-echo-server (https://github.com/tlaverdure/laravel-echo-server).

我还创建了一个小演示,它说明了使用 laravel-echo-server ( https://github.com/tlaverdure/laravel-echo-server)在 socket.io 中使用 Laravel echo 。

https://github.com/xparthxvorax/Larevel-echo-with-socket.io

https://github.com/xparthxvorax/Larevel-echo-with-socket.io

Might help someone.

可能会帮助某人。