与redis和socket io的laravel回声

时间:2016-11-02 16:35:06

标签: php node.js sockets laravel-5

我有一个事件SomeEvent.php

像这样:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class SomeEvent  implements ShouldBroadcast
{


    use InteractsWithSockets, SerializesModels;

   public $data;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($array)
    {
        $this->data = $array;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

我在bootstrap.js中包含以下内容并使用gulp

进行编译
import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://site.dev:6001'

});

window.Echo.private('channel-name')
    .listen('SomeEvent', (e) => {
        console.log(e);
});

然后我安装了tlaverdure / laravel-echo-server,这是我的laravel-echo-server.json

{
    "appKey": "[generated]",
    "authHost": "http://site.dev",
    "authEndpoint": "/broadcasting/auth",
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": false,
    "host": "sitei.dev",
    "port": "6001",
    "referrers": [],
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": ""
}

现在当我使用laravel-echo-server启动laravel echo服务器启动它启动时非常好,但是当我启动上述事件时

event(new SomeEvent(json_encode(['name' => 'some-name'])));

我可以看到发布到redis的事件但是我的客户端控制台没有任何内容:我还在我的master.blade.php中包含socket io

以上也会发生通知

任何帮助都将受到高度赞赏。谢谢你们

1 个答案:

答案 0 :(得分:1)

您是否在BroadcastServiceProvider中为您的频道定义了身份验证规则?

如果你在laravel-echo-server.json中放入devMode,你看到有关连接和离开频道的任何信息吗?

我在尝试建立我的websocket连接时遇到了很多麻烦,但最终弄清楚了,我的代码看起来非常像你的。

祝你好运!