laravel-echo-server 用户未加入,未订阅套接字服务器

Posted

技术标签:

【中文标题】laravel-echo-server 用户未加入,未订阅套接字服务器【英文标题】:laravel-echo-server user not joining, not subscribed to the socket server 【发布时间】:2019-05-29 09:12:25 【问题描述】:

Laravel echo server is started 事件被广播,但用户无法加入频道,因此无法收听该事件。

1. ExampleEvent file:
class ExampleEvent implements ShouldBroadcast

    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    
        //
    

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    
        return new Channel('test-event');
    

    public function broadcastWith()
    
        return [
            'data' => 'key'
        ];
    


2. Laravel-echo-server.json file

    "authHost": "http://localhost",
    "authEndpoint": "/broadcasting/auth",
    "clients": [],
    "database": "redis",
    "databaseConfig": 
        "redis":  ,
        "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": ""
    


3. boostrap.js file laravel echo section

import Echo from 'laravel-echo'

window.io = require('socket.io-client');

window.Echo = new Echo(
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
);

window.Echo.channel('test-event')
    .listen('ExampleEvent', (e) => 
        console.log(e);
    );

4.Channels.php file
<?php

Broadcast::channel('App.User.id', function ($user, $id) 
    return (int) $user->id === (int) $id;
);

Broadcast::channel('test-event', function ($user, $id) 
    return true;
);

5.env file redis section

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

Output: When i open http://127.0.0.1:8000/test-broadcast and on other browser http://127.0.0.1:8000/ not shown that user joined the channel etc and event details is not listen on other browser. Also not getting any error on command line or console log.

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

version 1.5.0

⚠ Starting server in DEV mode...

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

Server ready!

Channel: test-event
Event: App\Events\ExampleEvent
Channel: test-event
Event: App\Events\ExampleEvent
Channel: test-event
Event: App\Events\ExampleEvent
Channel: test-event

【问题讨论】:

【参考方案1】:

我遇到了同样的问题。

试试这个版本"socket.io-client": "2.3.0"

同时调试队列php artisan queue:work sqs --sleep=3 --tries=3 如果有错误并解决错误

【讨论】:

谢谢你,我一直坚持添加这个,直到我看到你的评论,我说让我尝试更改 socket.io-client 的版本!【参考方案2】:

你必须选择聆听 1- 放置您的活动的完整命名空间

window.Echo.channel('test-event')
.listen('App\\Events\\ExampleEvent', (e) => 
    console.log(e);
);

2- 将此事件的名称放入事件文件中

public function broadcastAs()

    return "example-event";

在 Echo 中调用事件,在事件名称前加上(点)

window.Echo.channel('test-event')
.listen('.example-event', (e) => 
    console.log(e);
);

【讨论】:

以上是关于laravel-echo-server 用户未加入,未订阅套接字服务器的主要内容,如果未能解决你的问题,请参考以下文章

laravel-echo-server 404 尝试进行身份验证时

Laravel-echo-server 码头化问题

Laravel-echo-server 无法使用 https 协议启动生产

使用 Laravel Echo、laravel-echo-server 和 socket.io 广播将不起作用

Socket.io 在带有 redis 和 Laravel-echo-server 的 Web 浏览器 chrome 中没有显示任何内容

在 laravel-echo-server 中找不到模块“node-v46-linux-x64/node_sqlite3.node”错误