Skip to content

Commit

Permalink
feat: add more ice servers for TURN connection
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreBellas committed May 7, 2024
1 parent 535729e commit 5b49a05
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
18 changes: 18 additions & 0 deletions src/contexts/MultiplayerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
},
],
},
})
Expand Down Expand Up @@ -128,6 +144,8 @@ function multiplayerReducer(
}
}
case 'remove-connection': {
state.peerConnection?.close()

return {
...state,
peerConnection: undefined,
Expand Down
30 changes: 16 additions & 14 deletions src/views/Game/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -178,7 +181,7 @@ export default function MainPage() {
Alert.alert('Connection error', 'Connection had error with peer.')
}

resetMultiplayerStatus()
resetMultiplayerStatus(true)
})

// Close
Expand All @@ -194,7 +197,7 @@ export default function MainPage() {
)
}

resetMultiplayerStatus()
resetMultiplayerStatus(false)
})
},
[
Expand All @@ -218,7 +221,7 @@ export default function MainPage() {
)
}

resetMultiplayerStatus()
resetMultiplayerStatus(true)
return
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5b49a05

Please sign in to comment.