Skip to content

Commit

Permalink
checking for socket connection before initiating connection again.
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshuNegi755 committed Dec 4, 2020
1 parent 319527b commit 21647e7
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions client/src/components/Chat/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,26 @@ const Chat = ({ location, user }) => {
if(navigator.onLine) {
setOnline(true);

const { room, roomId } = queryString.parse(location.search);
socket = io(process.env.REACT_APP_SOCKET_ENDPOINT, {transports: ['websocket', 'polling', 'flashsocket']});
if(!socket.connected) {
const { room, roomId } = queryString.parse(location.search);
socket = io(process.env.REACT_APP_SOCKET_ENDPOINT, {transports: ['websocket', 'polling', 'flashsocket']});

setRoom(room);
setName(user.userName);
setRoomId(roomId);
const userName = user.userName;
setRoom(room);
setName(user.userName);
setRoomId(roomId);
const userName = user.userName;

socket.emit('join', { userName, room, roomId }, (error) => {
if(error) {
alert(error);
setRedirectTo404(false)
//alert(error);
} else {
axios.get(`${process.env.REACT_APP_BACKEND_API}/chat/${roomId}`)
.then(res => { setMessages(res.data) })
}
});
socket.emit('join', { userName, room, roomId }, (error) => {
if(error) {
alert(error);
setRedirectTo404(false)
//alert(error);
} else {
axios.get(`${process.env.REACT_APP_BACKEND_API}/chat/${roomId}`)
.then(res => { setMessages(res.data) })
}
});
}
} else { setOnline(false); }
}

Expand Down

0 comments on commit 21647e7

Please sign in to comment.