Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
reconnection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
HexaField committed Dec 27, 2023
1 parent 3dc0c9a commit 866575c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
73 changes: 73 additions & 0 deletions examples/InstanceConnection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react'

import { LocationInstanceState } from '@etherealengine/client-core/src/common/services/LocationInstanceConnectionService'
import { LocationIcons } from '@etherealengine/client-core/src/components/LocationIcons'
import { useLoadEngineWithScene, useNetwork } from '@etherealengine/client-core/src/components/World/EngineHooks'
import { useLoadLocation } from '@etherealengine/client-core/src/components/World/LoadLocationScene'
import { LocationSeed, LocationState } from '@etherealengine/client-core/src/social/services/LocationService'
import { SocketWebRTCClientNetwork } from '@etherealengine/client-core/src/transports/SocketWebRTCClientFunctions'
import { AuthService } from '@etherealengine/client-core/src/user/services/AuthService'
import { Button } from '@etherealengine/editor/src/components/inputs/Button'
import { NetworkState } from '@etherealengine/engine/src/networking/NetworkState'
import { LocationType } from '@etherealengine/engine/src/schemas/social/location.schema'
import { getMutableState, getState } from '@etherealengine/hyperflux'

globalThis.NetworkState = NetworkState

export default function InstanceConnection() {
useNetwork({ online: true })

useLoadLocation({ locationName: 'default' })

AuthService.useAPIListeners()

useLoadEngineWithScene()

/** Mimic a server choosing to close our connection */
const onNetworkDisconnect = () => {
const instanceID = Object.keys(getState(LocationInstanceState).instances)[0]
const network = getState(NetworkState).networks[instanceID] as SocketWebRTCClientNetwork
network.transport.primus.end()
}

const onNetworkLostConnection = () => {
const instanceID = Object.keys(getState(LocationInstanceState).instances)[0]
const network = getState(NetworkState).networks[instanceID] as SocketWebRTCClientNetwork
clearInterval(network.transport.heartbeat)
/** in 10 seconds, the server will end the connection to the client and remove it's peer */
}

/** */
const onLeaveLocation = () => {
getMutableState(LocationState).set({
locationName: null! as string,
currentLocation: {
location: LocationSeed as LocationType,
bannedUsers: [] as string[],
selfUserBanned: false,
selfNotAuthorized: false
},
invalidLocation: false
})
}

/** Mimic a server kicking us */
// const onNetworkKick = () => {}

/** Mimic a server banning us */
// const onNetworkBan = () => {}

/** Mimic us choosing to close a network */
// const onNetworkClose = () => {}

return (
<>
<div style={{ pointerEvents: 'all', position: 'absolute', top: '50%', left: '50%' }}>
<Button onClick={onNetworkDisconnect}>Disconnect Network</Button>
<Button onClick={onNetworkLostConnection}>Lose Connection</Button>
{/* <Button onClick={onLeaveLocation}>Leave Location</Button> */}
</div>
<LocationIcons />
</>
)
}
2 changes: 1 addition & 1 deletion examples/utils/avatar/loadAvatarHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,6 @@ export const loadAssetWithLoopAnimation = async (filename, position: Vector3, i:
activeClipIndex: 0,
animationPack: config.client.fileServer + '/projects/default-project/assets/animations/emotes/wave.fbx'
})
setComponent(entity, ModelComponent, { src: filename, generateBVH: false, avoidCameraOcclusion: true })
setComponent(entity, ModelComponent, { src: filename, cameraOcclusion: false })
return entity
}
4 changes: 2 additions & 2 deletions examples/utils/template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect } from 'react'
import { ARPlacement } from '@etherealengine/client-core/src/components/ARPlacement'
import { MediaIconsBox } from '@etherealengine/client-core/src/components/MediaIconsBox'
import { Shelves } from '@etherealengine/client-core/src/components/Shelves'
import { useLoadEngineWithScene, useOfflineNetwork } from '@etherealengine/client-core/src/components/World/EngineHooks'
import { useLoadEngineWithScene, useNetwork } from '@etherealengine/client-core/src/components/World/EngineHooks'
import { useLoadScene } from '@etherealengine/client-core/src/components/World/LoadLocationScene'
import { XRLoading } from '@etherealengine/client-core/src/components/XRLoading'
import { AvatarService } from '@etherealengine/client-core/src/user/services/AvatarService'
Expand All @@ -12,7 +12,7 @@ import './avatar/simulateMovement'

export function Template(props: { projectName?: string; sceneName?: string }) {
useLoadScene({ projectName: props.projectName ?? 'default-project', sceneName: props.sceneName ?? 'default' })
useOfflineNetwork()
useNetwork({ online: false })
useLoadEngineWithScene({ spectate: true })

useEffect(() => {
Expand Down

0 comments on commit 866575c

Please sign in to comment.