听不到Vue中Laravel Echo的活动

时间:2019-05-28 11:48:30

标签: laravel socket.io laravel-echo

我听不到Vue中Laravel Echo的活动,但似乎一切都很好。

我使用Laravel Echo + Socket.IO + VueJS

// bootstrap.js-设置连接

import Echo from 'laravel-echo';
window.io = require('socket.io-client');
if (typeof io !== 'undefined') {
    window.Echo = new Echo({
        broadcaster: 'socket.io',
        host: window.location.hostname + ':6001'
    });
}

// app.js-收听频道

Echo.private(`success_load.1`)
    .listen('DataLoadFinishedNotification', (e) => {
    console.log(e);
});

// channels.php-私人频道身份验证

Broadcast::channel('success_load.1', function () {
    return true;
});

// TestController-初始化事件

public function Test(Request $request) {
  event(new DataLoadFinishedNotification('some data'));
}

// DataLoadFinishedNotification.php-广播到频道

namespace App\Events;

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

class DataLoadFinishedNotification
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $date;
    public function __construct($date)
    {
        $this->date = $date;
    }
    public function broadcastOn()
    {
        return new PrivateChannel('success_load.1');
    }

}

// laravel-echo-server.json-Laravel回声确认

{
    "authHost": "http://127.0.0.1:8000",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "85bf5ec3ad689355",
            "key": "af0415d10c2ed753e1cd93b171d6bfbf"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "127.0.0.1"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "",
        "allowMethods": "",
        "allowHeaders": ""
    }
}

Laravel Echo Server工作正常...

L A R A V E L  E C H O  S E R V E R

version 1.5.5

⚠ Starting server in DEV mode...

✔  Running at localhost on port 6001
✔  Channels are ready.
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

[14:33:06] - Preparing authentication request to: http://127.0.0.1:8000
[14:33:06] - Sending auth request to: http://127.0.0.1:8000/broadcasting/auth

[14:33:08] - yVjFZfCoFiUNJTlPAAAB authenticated for: private-success_load.1
[14:33:08] - yVjFZfCoFiUNJTlPAAAB joined channel: private-success_load.1

0 个答案:

没有答案