Skip to content

Commit

Permalink
refactor: Use session id as the peer group id
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Dec 24, 2023
1 parent 0abc260 commit 261623e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
18 changes: 10 additions & 8 deletions xmcl-keystone-ui/src/composables/peers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GameProfileAndTexture, PeerServiceKey, PeerState } from '@xmcl/runtime-api'
import { BaseServiceKey, GameProfileAndTexture, PeerServiceKey, PeerState } from '@xmcl/runtime-api'
import { InjectionKey, Ref } from 'vue'
import { PeerGroup } from './peerGroup'
import { useService } from './service'
Expand All @@ -9,6 +9,7 @@ export const kPeerState: InjectionKey<ReturnType<typeof usePeerState>> = Symbol(
export function usePeerState(gameProfile: Ref<GameProfileAndTexture>) {
const { getPeerState, initiate, on, setRemoteDescription } = useService(PeerServiceKey)
const { state } = useState(getPeerState, PeerState)
const { getSessionId } = useService(BaseServiceKey)
const connections = computed(() => state.value?.connections || [])

const group = ref('')
Expand All @@ -17,24 +18,25 @@ export function usePeerState(gameProfile: Ref<GameProfileAndTexture>) {
let _group: PeerGroup | undefined
let _id = ''

getSessionId().then((s) => {
_id = s
})

on('connection-local-description', ({ description, type }) => {
_group?.sendLocalDescription(description.id, description.sdp, type, description.candidates)
})

function joinGroup(groupId?: string) {
async function joinGroup(groupId?: string) {
if (!groupId) {
const buf = new Uint16Array(1)
window.crypto.getRandomValues(buf)
groupId = gameProfile.value.name + '@' + buf[0]
}
if (!_id) {
try {
if (typeof window.crypto.randomUUID === 'function') {
_id = window.crypto.randomUUID()
} catch {
const buf = new Uint16Array(16)
window.crypto.getRandomValues(buf)
const str = [...buf].map(v => v.toString(16)).join('')
_id = str
} else {
_id = await getSessionId()
}
}
_group = new PeerGroup(groupId, _id)
Expand Down
2 changes: 2 additions & 0 deletions xmcl-runtime-api/src/services/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface Environment extends Platform {
export interface BaseService {
validateDataDictionary(path: string): Promise<undefined | 'noperm' | 'bad' | 'nondictionary' | 'exists'>

getSessionId(): Promise<string>

getSettings(): Promise<MutableState<Settings>>
/**
* Get the environment of the launcher
Expand Down
4 changes: 4 additions & 0 deletions xmcl-runtime/base/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class BaseService extends AbstractService implements IBaseService {
})
}

getSessionId() {
return this.app.registry.get(kClientToken)
}

getGameDataDirectory(): Promise<string> {
return this.app.getGameDataPath()
}
Expand Down

0 comments on commit 261623e

Please sign in to comment.