前端获取不到socket id
Unable to get the socket id on the front end
我需要在前端获取每个套接字的 ID,以便稍后我可以区分每个连接。当我打印 socketRef.current 时,我可以清楚地看到其中的内容,甚至可以看到其中的“id”,但只有“id”是我无法从那里打印的东西(如果我打印“io”或“ids”它可以工作).为什么我不能打印“id”字段?有没有办法从后端正确获取id?
const socketRef = useRef();
useEffect(() => {
socketRef.current = io.connect('http://localhost:5000')
console.log(socketRef.current.id)
}, []);
我认为需要先等待 connect
:
useEffect(() => {
socketRef.current = io.connect('http://localhost:5000')
socketRef.current.on('connect', ()=> {
console.log(socketRef.current.socket.sessionid);
});
}, []);
连接 io 和套接字 connect
获取 socket.id
const socket = io.connect('http://localhost:5000');
socket.on("connect", () => {
console.log(socket.id);
});
我需要在前端获取每个套接字的 ID,以便稍后我可以区分每个连接。当我打印 socketRef.current 时,我可以清楚地看到其中的内容,甚至可以看到其中的“id”,但只有“id”是我无法从那里打印的东西(如果我打印“io”或“ids”它可以工作).为什么我不能打印“id”字段?有没有办法从后端正确获取id?
const socketRef = useRef();
useEffect(() => {
socketRef.current = io.connect('http://localhost:5000')
console.log(socketRef.current.id)
}, []);
我认为需要先等待 connect
:
useEffect(() => {
socketRef.current = io.connect('http://localhost:5000')
socketRef.current.on('connect', ()=> {
console.log(socketRef.current.socket.sessionid);
});
}, []);
连接 io 和套接字 connect
获取 socket.id
const socket = io.connect('http://localhost:5000');
socket.on("connect", () => {
console.log(socket.id);
});