laravel echo不监听事件广播

dohp0rv5  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(264)

我面临这样一个问题,即laravelecho不能监听服务器上的事件广播。但是,当我选中在ws-window上调试时,我看到了从服务器发送的数据。我不知道为什么在通道名称和事件名称正确的情况下,我的控制台中不接收任何事件。此处显示图像描述
这里有我的代码输入图像描述
我已经在docker的laravel echo服务器窗口中进行了检查,在这里输入图像描述,其中有一些类似的内容,频道名称(post\u created)和事件名称(post created)是正确的,请帮助我解决此问题,感谢avandce!

import React, { useCallback, useState } from 'react';
import {echoNotification} from '../../../utils/echo'

const Topbar = ({ refs }: any) => {

  React.useEffect(() => {

      //This function doesn't work every time an event is sent from server
      echoNotification()
  })

  return (
    <>
    </>
  );
};

export default Topbar;
import Echo from 'laravel-echo';
import * as io from 'socket.io-client';

const echo = new Echo({
    auth: {
        headers: {
            'Authorization': 'Bearer ' + localStorage.getItem('token'),
        }
    },
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
    client: io,
});

export function echoNotification() {
    //Not working, it didn't listen event Post_Created on channel post_created
    echo.channel('post_created')
        .listen('PostCreated', (res) => {
            console.log(res)
        })
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题