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

Peer Notification Cleanup #8004

Merged
merged 5 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,11 @@ export async function onConnectToInstance(network: SocketWebRTCClientNetwork) {

function peerUpdateHandler(peers: Array<PeersUpdateType>) {
for (const peer of peers) {
const hasPeer = network.peers.get(peer.peerID)
NetworkPeerFunctions.createPeer(network, peer.peerID, peer.peerIndex, peer.userID, peer.userIndex, peer.name)
if (network.topic === NetworkTopics.world && !hasPeer && network.peers.get(peer.peerID)) {
NotificationService.dispatchNotify(`${peer.name} ${t('common:toast.joined')}`, { variant: 'default' })
}
}
for (const [peerID, peer] of network.peers)
if (!peers.find((p) => p.peerID === peerID)) {
NetworkPeerFunctions.destroyPeer(network, peerID)
if (network.topic === NetworkTopics.world) {
const name = getState(WorldState).userNames[peer.userId]
NotificationService.dispatchNotify(`${name} ${t('common:toast.left')}`, { variant: 'default' })
}
}
if (network.topic === NetworkTopics.media) removePeerMediaChannels(SelfPeerID)
logger.info('Updated peers %o', { topic: network.topic, peers })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { PeerID } from '@etherealengine/common/src/interfaces/PeerID'
import { UserId } from '@etherealengine/common/src/interfaces/UserId'
import { dispatchAction, getMutableState } from '@etherealengine/hyperflux'
import { Action, ResolvedActionType } from '@etherealengine/hyperflux/functions/ActionFunctions'
import { getState } from '@etherealengine/hyperflux/functions/StateFunctions'
import { getState, none } from '@etherealengine/hyperflux/functions/StateFunctions'

import { Engine } from '../../ecs/classes/Engine'
import { EngineActions } from '../../ecs/classes/EngineState'
import { removeEntity } from '../../ecs/functions/EntityFunctions'
import { Network } from '../classes/Network'
import { Network, NetworkTopics } from '../classes/Network'
import { WorldState } from '../interfaces/WorldState'
import { NetworkState, updateNetwork } from '../NetworkState'
import { WorldNetworkAction } from './WorldNetworkAction'
Expand Down Expand Up @@ -43,8 +43,11 @@ function createPeer(
//TODO: remove this once all network state properties are reactively set
updateNetwork(network)

const worldState = getMutableState(WorldState)
worldState.userNames[userID].set(name)
// TODO: we probably want an explicit config for detecting a non-user peer
if (peerID !== 'server') {
const worldState = getMutableState(WorldState)
worldState.userNames[userID].set(name)
}
}

function destroyPeer(network: Network, peerID: PeerID) {
Expand Down
1 change: 1 addition & 0 deletions packages/engine/src/networking/interfaces/WorldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type AvatarProps = typeof matchesAvatarProps._TYPE
export const WorldState = defineState({
name: 'WorldState',
initial: () => ({
/** a history of user names - does not get cleaned up upon a user leaving the world */
userNames: {} as Record<UserId, string>,
userAvatarDetails: {} as Record<UserId, AvatarProps>
})
Expand Down
12 changes: 6 additions & 6 deletions packages/engine/src/networking/systems/OutgoingActionSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export const sendActionsAsPeer = (network: Network) => {
if (!network.ready) return
const actions = [...Engine.instance.store.actions.outgoing[network.topic].queue]
if (!actions.length) return
for (const peerID of network.transport.peers) {
network.transport.messageToPeer(network.hostPeerID, {
type: MessageTypes.ActionData.toString(),
/*encode(*/ data: actions
}) //)
}
// for (const peerID of network.transport.peers) {
network.transport.messageToPeer(network.hostPeerID, {
type: MessageTypes.ActionData.toString(),
/*encode(*/ data: actions
}) //)
// }
clearOutgoingActions(network.topic)
}

Expand Down