Moved urls to environment and turn server retreval

This commit is contained in:
Mitchell McCaffrey 2020-05-03 23:18:45 +10:00
parent 16b27a7058
commit 79b46e0b5a
3 changed files with 19 additions and 4 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
REACT_APP_BROKER_URL=http://localhost:9000
REACT_APP_ICE_SERVERS_URL=http://localhost:9000/iceservers

2
.env.production Normal file
View File

@ -0,0 +1,2 @@
REACT_APP_BROKER_URL=https://agent.owlbear.rodeo
REACT_APP_ICE_SERVERS_URL=http://agent.owlbear.rodeo/iceservers

View File

@ -6,7 +6,7 @@ import Peer from "../helpers/Peer";
import AuthContext from "../contexts/AuthContext";
const socket = io("https://agent.owlbear.rodeo");
const socket = io(process.env.REACT_APP_BROKER_URL);
function useSession(
partyId,
@ -18,9 +18,16 @@ function useSession(
onPeerError
) {
const { password, setAuthenticationStatus } = useContext(AuthContext);
const [iceServers, setIceServers] = useState([]);
useEffect(() => {
socket.emit("join party", partyId, password);
async function joinParty() {
const response = await fetch(process.env.REACT_APP_ICE_SERVERS_URL);
const data = await response.json();
setIceServers(data.iceServers);
socket.emit("join party", partyId, password);
}
joinParty();
}, [partyId, password]);
const [peers, setPeers] = useState({});
@ -127,7 +134,11 @@ function useSession(
// Setup event listeners for the socket
useEffect(() => {
function addPeer(id, initiator, sync) {
const connection = new Peer({ initiator, trickle: false });
const connection = new Peer({
initiator,
trickle: false,
config: { iceServers },
});
setPeers((prevPeers) => ({
...prevPeers,
@ -178,7 +189,7 @@ function useSession(
socket.removeListener("signal", handleSignal);
socket.removeListener("auth error", handleAuthError);
};
}, [peers, setAuthenticationStatus]);
}, [peers, setAuthenticationStatus, iceServers]);
return { peers, socket };
}