无法从socket.io获取响应

uajslkp6  于 2021-06-08  发布在  Redis
关注(0)|答案(1)|浏览(411)

我试图创建一个简单的聊天应用程序,我试图听私人频道,但我不能听它。不知道我在哪里犯的错误我的代码如下。
文件:chat.vue

export default {
        props: {
            user: {
                type: Object,
                required: true
            }
        },
        data() {
            return {
                selectedContact: null,
                messages: [],
                contacts: []
            };
        },
        mounted() {
            window.Echo.private(`chats.${this.user.id}`)
            .listen('NewChatMessage', (e) => {
                console.log('heared');
                // this.handleIncoming(e.message);
            });

            console.log('mounted');

            axios.get('/chat/contacts')
            .then((response) => {
                this.contacts = response.data;
            });
        },
        methods: {
            startConversationWith(contact) {
                axios.get(`/chat/conversation/${contact.id}`)
                .then((response) => {
                    this.messages = response.data;
                    this.selectedContact = contact;
                });
            },
            saveNewMessage(text) {
                this.messages.push(text);
            },
            handleIncoming(message) {
                if(this.selectedContact && this.selectedContact.id == message.from) {
                    this.saveNewMessage(message);
                }
            }
        },
        components: {Conversation, ContactsList}
    }

文件名:routes/channels.php

Broadcast::channel('chats.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

文件名:app/events/newchatmessage.php

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

    public $chat;

    public $broadcastQueue = 'chat';

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

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

    public function broadcastWith()
    {
        return ["message" => $this->chat];
    }
}

在保存时,我这样称呼事件 broadcast(new NewChatMessage($chat)); 我的堆栈如下所示,
操作系统:windows 10
服务器:apache2
数据库:mysql
缓存:redis

vsmadaxz

vsmadaxz1#

我发现laravel在redis的config/database.php文件中有一个前缀options config。我在redis的选项中注解了前缀。然后它开始工作。

'redis' => [

        'client' => env('REDIS_CLIENT', 'phpredis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            // 'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_database_'),
        ],

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
        ],

        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_CACHE_DB', '1'),
        ],

    ],

这可能对将来的人有所帮助。

相关问题