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

Added error handling for instance provisioning returning a shutting-down instance #7000

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 77 additions & 14 deletions packages/client-core/src/transports/SocketWebRTCClientNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ import * as mediasoupClient from 'mediasoup-client'
import { Consumer, DataProducer, Transport as MediaSoupTransport, Producer } from 'mediasoup-client/lib/types'
import { io as ioclient, Socket } from 'socket.io-client'

import { Channel } from '@xrengine/common/src/interfaces/Channel'
import { UserId } from '@xrengine/common/src/interfaces/UserId'
import multiLogger from '@xrengine/common/src/logger'
import { Engine } from '@xrengine/engine/src/ecs/classes/Engine'
import { Network } from '@xrengine/engine/src/networking/classes/Network'
import { MessageTypes } from '@xrengine/engine/src/networking/enums/MessageTypes'
import { clearOutgoingActions } from '@xrengine/hyperflux'
import { clearOutgoingActions, dispatchAction } from '@xrengine/hyperflux'
import ActionFunctions, { Action, Topic } from '@xrengine/hyperflux/functions/ActionFunctions'

import {
accessLocationInstanceConnectionState,
LocationInstanceConnectionAction,
LocationInstanceConnectionService
} from '../common/services/LocationInstanceConnectionService'
import {
accessMediaInstanceConnectionState,
MediaInstanceConnectionAction,
MediaInstanceConnectionService
} from '../common/services/MediaInstanceConnectionService'
import { accessChatState } from '../social/services/ChatService'
import { accessLocationState } from '../social/services/LocationService'
import { accessAuthState } from '../user/services/AuthService'
import { instanceserverHost } from '../util/config'
import { onConnectToInstance } from './SocketWebRTCClientFunctions'
Expand All @@ -25,6 +38,46 @@ const promisedRequest = (socket: Socket) => {
}
}

const handleFailedConnection = (locationConnectionFailed) => {
if (locationConnectionFailed) {
const currentLocation = accessLocationState().currentLocation.location
const locationInstanceConnectionState = accessLocationInstanceConnectionState()
const instanceId = Engine.instance.currentWorld._worldHostId
if (!locationInstanceConnectionState.instances[instanceId].connected.value) {
dispatchAction(LocationInstanceConnectionAction.disconnect({ instanceId }))
LocationInstanceConnectionService.provisionServer(
currentLocation.id.value,
instanceId || undefined,
currentLocation.sceneId.value
)
}
} else {
const mediaInstanceConnectionState = accessMediaInstanceConnectionState()
const instanceId = Engine.instance.currentWorld._mediaHostId
if (!mediaInstanceConnectionState.instances[instanceId].connected.value) {
dispatchAction(MediaInstanceConnectionAction.disconnect({ instanceId }))
const authState = accessAuthState()
const selfUser = authState.user
const chatState = accessChatState()
const channelState = chatState.channels
const channels = channelState.channels.value as Channel[]
const channelEntries = Object.values(channels).filter((channel) => !!channel) as any
const instanceChannel = channelEntries.find(
(entry) => entry.instanceId === Engine.instance.currentWorld.worldNetwork?.hostId
)
if (instanceChannel) {
MediaInstanceConnectionService.provisionServer(instanceChannel?.id!, true)
} else {
const partyChannel = Object.values(chatState.channels.channels.value).find(
(channel) => channel.channelType === 'party' && channel.partyId === selfUser.partyId.value
)
MediaInstanceConnectionService.provisionServer(partyChannel?.id!, false)
}
}
}
return
}

export class SocketWebRTCClientNetwork extends Network {
constructor(hostId: UserId, topic: Topic) {
super(hostId, topic)
Expand Down Expand Up @@ -96,23 +149,33 @@ export class SocketWebRTCClientNetwork extends Network {
if (locationId) delete query.channelId
if (channelId) delete query.locationId

if (globalThis.process.env['VITE_LOCAL_BUILD'] === 'true') {
this.socket = ioclient(`https://${ipAddress as string}:${port.toString()}`, {
query
})
} else if (process.env.APP_ENV === 'development') {
this.socket = ioclient(`${ipAddress as string}:${port.toString()}`, {
query
})
} else {
this.socket = ioclient(instanceserverHost, {
path: `/socket.io/${ipAddress as string}/${port.toString()}`,
query
})
try {
if (globalThis.process.env['VITE_LOCAL_BUILD'] === 'true') {
this.socket = ioclient(`https://${ipAddress as string}:${port.toString()}`, {
query
})
} else if (process.env.APP_ENV === 'development') {
this.socket = ioclient(`${ipAddress as string}:${port.toString()}`, {
query
})
} else {
this.socket = ioclient(instanceserverHost, {
path: `/socket.io/${ipAddress as string}/${port.toString()}`,
query
})
}
} catch (err) {
logger.error(err)
return handleFailedConnection(locationId != null)
}
this.request = promisedRequest(this.socket)

const connectionFailTimeout = setTimeout(() => {
return handleFailedConnection(locationId != null)
}, 3000)

this.socket.on('connect', () => {
clearTimeout(connectionFailTimeout)
if (this.reconnecting) {
this.reconnecting = false
;(this.socket as any)._connected = false
Expand Down