Laravel echo服务器客户端未接收事件

时间:2017-02-23 14:02:23

标签: laravel socket.io laravel-echo

我使用此软件包(https://github.com/tlaverdure/laravel-echo-server)设置了一个小型Laravel Echo Server。我有以下设置:

// app.js

import Echo from "laravel-echo";

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

window.Echo.channel('documents-sharing')
    .listen("Api\\DocumentShared", (event) => {
    console.log(event);
});

// server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();

http.listen(3000, function(){
    console.log('Listening on Port 3000');
});

redis.subscribe('documents-sharing', function(err, count) {
    console.log("subscribed " + count);
});

redis.on('message', function(channel, message) {
    message = JSON.parse(message);

    console.log('Message Received: ', channel, message);

    console.log(io.to(channel).emit(message.event, message.data));
});

// laravel-echo-server.json
{
    "authHost": "http://localhost",
    "clients": [
        {
            "appId": "986bb8954bb1e2e9",
            "key": "58ca6a205718590ad5fd88a1afc1877c"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "localhost"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "localhost",
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": ""
}

// DocumentShared.php
class DocumentShared implements ShouldBroadcast, ShouldBroadcastNow
{
    use SerializesModels, InteractsWithSockets;
    /**
     * @var User
     */
    public $user;
    /**
     * @var Resource
     */
    public $resource;
    /**
     * @var Share
     */
    public $share;

    /**
     * Create a new event instance.
     *
     * @param User $user
     * @param Resource $resource
     * @param Share $share
     */
    public function __construct(User $user, Resource $resource, Share $share)
    {
        $this->user = $user;
        $this->resource = $resource;
        $this->share = $share;
    }

    public function broadcastOn()
    {
        return new Channel("documents-sharing");
    }
}

// app.blade.php
<script src="{{ asset('js/socket.io.min.js') }}"></script>
<script src="{{ asset('js/home.js') }}"></script>

之后我跑:

gulp watch
redis-server
node server.js
laravel-echo-server start
php artisan queue:listen

/notify路线被击中时,我会发射该事件。

Route::get('notify', function() {
    $user = User::first();
    $resource = Resource::first();
    $share = Share::first();

    event(new DocumentShared($user, $resource, $share));

    return 'done';
});

我可以看到服务器已收到该事件但它没有显示在队列中。

terminal output

2 个答案:

答案 0 :(得分:1)

如果您使用的是更新版本,请尝试降级到 "socket.io-client": "2.3.0"。经过几天的寻找解决方案,这为我解决了这个问题。

答案 1 :(得分:0)

问题是客户端无法连接到服务器,因为端口没有响应。具有讽刺意味的是,我刚刚从host配置文件中删除了laravel-echo-server.json密钥,然后重新运行服务器。一切都像魅力一样!