专用频道无法与Laravel回显服务器一起使用 [英] Private channel not working with Laravel echo server

查看:138
本文介绍了专用频道无法与Laravel回显服务器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制台上收到此JS错误:

I'm getting this JS error on the console:

app.js:167未捕获的ReferenceError:未定义receiveId

app.js:167 Uncaught ReferenceError: receiverId is not defined

这是我完整的代码:

PrivateChatController:

event(new PrivateMessageEvent($chat, $receiverId));

PrivateMessageEvent:

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;
use App\User;
use App\PrivateChat;

class PrivateMessageEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('private-chat.' . $this->receiverId);
    }
}

Bootstrap.js

import Echo from "laravel-echo"

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

window.Echo.private(`private-chat.${receiverId}`)
    .listen('PrivateMessageEvent', (e) => {
        console.log(e);
});

channels.php

Broadcast::channel('private-chat.{receiverId}', function ($user, $receiverId) {
    return true; // this is just for debugging to allow anyone to listen on this channel
    //return $user->id === $receiverId;
});

laravel-echo-server.json

{
    "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": ""
}

在后台 queue:work laravel-echo-server 已在运行

在触发该事件时,我正在laravel-echo-server控制台上收到此消息:

Upon firing that event, I'm getting this message on the laravel-echo-server console:

Channel: private-private-chat.
Event: App\Events\PrivateMessageEvent
CHANNEL private-private-chat.

注释:

  • 我可以成功收听公共频道.私人频道的唯一问题.

  • I'm successfully able to listen to the public channel. The only issue with the private channel.

使用最新的Laravel版本,即5.4

Using latest Laravel version i.e 5.4

我已经按照官方文档做了所有事情:

I have done all the things as per the official docs:

https://laravel.com/docs/master/广播

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

我也在github repo上提出了问题:

I have also raised issue on github repo:

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

我花了十多个小时,尽我所能,但没有运气.

I have spent more than 10 hours and tried everything I could, but no luck.

谢谢

推荐答案

尝试更改此行:

$this->$receiverId = $receiverId;

此行:

$this->receiverId = $receiverId;

在您的PrivateMessageEvent __construct()

In your PrivateMessageEvent __construct()

更新:

尝试使用像这样的固定频道ID:

Try to use use a fixed channel id like this:

window.Echo.private('private-chat.1')

我建议您也使用状态显示频道,它也是私有的,但具有更多功能,即:

I suggest you also to use presence channel, are private too but with more features, i.e.:

Echo.join('private-chat.1')
   .listen('PrivateMessageEvent', (e) => {
    console.log(e);
});

如果您使用的是动态频道编号,即:

If you use a dinamic channel number like you use, i.e.:

window.Echo.private(`private-chat.${receiverId}`)

您必须在JavaScript中为receiverId赋一个值,该声明是一个通用的房间侦听器,但应定义receiveId,$ {receiverId}是字符串插值.

You have to give receiverId a value in javascript, this declaration is a generic room listener but receiverId should be defined, ${receiverId} is a string interpolation.

例如,可以使用刀片语法在加入app.js之前在模板中定义接收器ID:

You can define receiverId in the template before inluding app.js, for example, using blade syntax:

<script>
   receiverId = {{ $receiverId }};
</script>

另一种想法:我想清楚一点,在以上所有代码中,receiverId代表客户端希望加入的聊天室的ID,而不是接收用户的ID.

Another think: I want to be clear that, in all the code above, receiverId represent the id of the chat/room a client want join to not the ID of the receiving user.

这篇关于专用频道无法与Laravel回显服务器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆