我面临的问题是laravel-echo无法监听服务器广播的事件。但是,当我在ws窗口上检查调试时,我看到此数据是从服务器发送的。我不知道为什么在频道名称和事件名称正确的情况下我的控制台中没有收到任何事件。 image description here
有我的代码 enter image description here
我已经检查了docker enter image description here中的laravel-echo-server窗口,其中包含类似以下内容,通道名称(post_created)和事件名称(PostCreated)正确 请帮助我解决此问题,谢谢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)
})
}