Uncaught TypeError: Cannot read properties of undefined (reading 'emit')
//클라이언트 웹소켓 연결 코드 파일 export const registerNewUser = (username) => { console.log(username); console.log(socket.emit) socket.emit('newUser', { username: username, socketId: socket.id }); } //registerNewUser함수를 사용하는 컴포넌트 const handlerJoinButton = () => { dispatch(dashboardActions(username)); history.push('/dashboard'); registerNewUser(username);
버튼을 누를 때 오류가 발생
"dependencies": {
"express": "^4.18.2",
"socket.io": "^4.5.4"
}
//Before
import socketClient from 'socket.io-client'
const SERVER = 'http://lacalhost:5500/'
let socket
export const connectWithWebSocket = () => {
socket = socketClient(SERVER)
socket.on('connection', (socket) => {
console.log('succesfully connected with wss server')
console.log('socket.id')
})
}
//After
import {io} from 'socket.io-client';
export let socket = io("http://localhost:5500", { transports: ["websocket"]});
export const initSocketConnection = () => {
return socket.connect();
};