From 75e2a591b30eda964cc219ccdf93dbbbc011daa3 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Sun, 28 Jun 2020 09:58:07 +1000 Subject: [PATCH] Added error handling for peer errors when socket is fine --- src/docs/faq/connection.md | 4 ++++ src/helpers/Peer.js | 21 ++++++++++++--------- src/helpers/useSession.js | 25 ++++++++++++++----------- src/routes/Game.js | 2 ++ 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/docs/faq/connection.md b/src/docs/faq/connection.md index d69a5a0..8c035f7 100644 --- a/src/docs/faq/connection.md +++ b/src/docs/faq/connection.md @@ -17,3 +17,7 @@ The good news is that Safari will still work if the two devices are connected to ### WebRTC not supported. Owlbear Rodeo uses WebRTC to communicate between players. Ensure your browser supports WebRTC. A list of supported browsers can be found [here](https://caniuse.com/#feat=rtcpeerconnection). + +### Unable to connect to party. + +This can happen when your internet connection is stable but a peer to peer connection wasn't able to be established between party members. Refreshing the page can help in fixing this. diff --git a/src/helpers/Peer.js b/src/helpers/Peer.js index 680506b..5a5a385 100644 --- a/src/helpers/Peer.js +++ b/src/helpers/Peer.js @@ -46,16 +46,19 @@ class Peer extends SimplePeer { } send(data) { - const packedData = encode(data); - - if (packedData.byteLength > MAX_BUFFER_SIZE) { - const chunks = this.chunk(packedData); - for (let chunk of chunks) { - super.send(encode(chunk)); + try { + const packedData = encode(data); + if (packedData.byteLength > MAX_BUFFER_SIZE) { + const chunks = this.chunk(packedData); + for (let chunk of chunks) { + super.send(encode(chunk)); + } + return; + } else { + super.send(packedData); } - return; - } else { - super.send(packedData); + } catch (error) { + console.error(error); } } diff --git a/src/helpers/useSession.js b/src/helpers/useSession.js index 7d52331..22076f6 100644 --- a/src/helpers/useSession.js +++ b/src/helpers/useSession.js @@ -143,16 +143,19 @@ function useSession( // Setup event listeners for the socket useEffect(() => { function addPeer(id, initiator, sync) { - const connection = new Peer({ - initiator, - trickle: true, - config: { iceServers }, - }); - - setPeers((prevPeers) => ({ - ...prevPeers, - [id]: { id, connection, initiator, sync }, - })); + try { + const connection = new Peer({ + initiator, + trickle: true, + config: { iceServers }, + }); + setPeers((prevPeers) => ({ + ...prevPeers, + [id]: { id, connection, initiator, sync }, + })); + } catch (error) { + onPeerError && onPeerError({ error }); + } } function handlePartyMemberJoined(id) { @@ -214,7 +217,7 @@ function useSession( socket.off("signal", handleSignal); socket.off("auth error", handleAuthError); }; - }, [peers, setAuthenticationStatus, iceServers, joinParty]); + }, [peers, setAuthenticationStatus, iceServers, joinParty, onPeerError]); return { peers, socket, connected }; } diff --git a/src/routes/Game.js b/src/routes/Game.js index 6c1a2d3..007c48c 100644 --- a/src/routes/Game.js +++ b/src/routes/Game.js @@ -423,6 +423,8 @@ function Game() { console.error(error.code); if (error.code === "ERR_WEBRTC_SUPPORT") { setPeerError("WebRTC not supported"); + } else if (error.code === "ERR_CREATE_OFFER") { + setPeerError("Unable to connect to party"); } }