From 5b49a05cb2938e0f43eb4881ed5dd32c504aa614 Mon Sep 17 00:00:00 2001 From: Alexandre Batistella Bellas Date: Tue, 7 May 2024 15:30:37 -0300 Subject: [PATCH] feat: add more ice servers for TURN connection --- src/contexts/MultiplayerContext.tsx | 18 +++++++++++++++++ src/views/Game/MainPage.tsx | 30 +++++++++++++++-------------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/contexts/MultiplayerContext.tsx b/src/contexts/MultiplayerContext.tsx index 7f05389..e1988c1 100644 --- a/src/contexts/MultiplayerContext.tsx +++ b/src/contexts/MultiplayerContext.tsx @@ -70,11 +70,27 @@ export default function MultiplayerProvider({ const peer = new Peer(currPlayerId, { config: { iceServers: [ + { urls: 'stun:stun.relay.metered.ca:80' }, { urls: 'turn:global.relay.metered.ca:80', username: 'fad8157c3c3294f670891975', credential: 'VCQqHCzv9xZqG+Y/', }, + { + urls: 'turn:global.relay.metered.ca:80?transport=tcp', + username: 'fad8157c3c3294f670891975', + credential: 'VCQqHCzv9xZqG+Y/', + }, + { + urls: 'turn:global.relay.metered.ca:443', + username: 'fad8157c3c3294f670891975', + credential: 'VCQqHCzv9xZqG+Y/', + }, + { + urls: 'turns:global.relay.metered.ca:443?transport=tcp', + username: 'fad8157c3c3294f670891975', + credential: 'VCQqHCzv9xZqG+Y/', + }, ], }, }) @@ -128,6 +144,8 @@ function multiplayerReducer( } } case 'remove-connection': { + state.peerConnection?.close() + return { ...state, peerConnection: undefined, diff --git a/src/views/Game/MainPage.tsx b/src/views/Game/MainPage.tsx index b71ff3f..000e45a 100644 --- a/src/views/Game/MainPage.tsx +++ b/src/views/Game/MainPage.tsx @@ -107,17 +107,20 @@ export default function MainPage() { // #endregion // #region Callbacks - const resetMultiplayerStatus = useCallback(() => { - setIsMultiplayer(false) - setIsConfiguringMultiplayer(false) - setHasConnectionError(true) - setIsConnectingToPlayer(false) + const resetMultiplayerStatus = useCallback( + (hasError: boolean) => { + setIsMultiplayer(false) + setIsConfiguringMultiplayer(false) + setHasConnectionError(hasError) + setIsConnectingToPlayer(false) - peerConnection?.close() + peerConnection?.close() - multiplayerDispatch({ type: 'remove-connection' }) - boardDispatch({ type: 'restart' }) - }, [multiplayerDispatch, boardDispatch, peerConnection]) + multiplayerDispatch({ type: 'remove-connection' }) + boardDispatch({ type: 'restart' }) + }, + [multiplayerDispatch, boardDispatch, peerConnection], + ) const multiplayerConfigure = useCallback( (peerConnection: DataConnection) => { @@ -178,7 +181,7 @@ export default function MainPage() { Alert.alert('Connection error', 'Connection had error with peer.') } - resetMultiplayerStatus() + resetMultiplayerStatus(true) }) // Close @@ -194,7 +197,7 @@ export default function MainPage() { ) } - resetMultiplayerStatus() + resetMultiplayerStatus(false) }) }, [ @@ -218,7 +221,7 @@ export default function MainPage() { ) } - resetMultiplayerStatus() + resetMultiplayerStatus(true) return } @@ -426,14 +429,13 @@ export default function MainPage() { if (!isMultiplayer) return console.log('disconnected!', currentId) - if (Platform.OS === 'web') { alert('Disconnected from peer.') } else { Alert.alert('Disconnected', 'You disconnected from the peer.') } - resetMultiplayerStatus() + resetMultiplayerStatus(false) }) }, [ peerInstance,